django.db.utils.IntegrityError: The row in table 'food_item' with primary key '1' has an invalid foreign key: food_item.user_name_id contains a value '1' that does not have a corresponding value in auth_user.id.
# code from models.py of my app
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Item(models.Model):
def __str__(self): # this bit of code is written for become avilable the name of the items insted of id. in shell
return self.item_name
user_name = models.ForeignKey( User, on_delete = models.CASCADE, default = 1 )
item_name = models.CharField(max_length=200)
item_disc = models.CharField(max_length=200)
item_price = models.IntegerField()
item_image = models.CharField(max_length=500, default='')
the problem is : User.objects.get( pk = 1 ) and pk = 2 are not exist. I had deleted the user profile pk = 1 and 2 because I had forgotten the password. This meant django could't find the user with the id of 1 .
how to fix the issue ?
im getting an error with migrating to my database.
The error is ' (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.'
STATUS = (('active', 'active'), ('inactive', 'inactive')),
LABEL = (('new', 'new'), ('popular', 'popular'))
class Category(models.Model):
name = models.CharField(max_length=200)
image = models.CharField(max_length=200)
slug = models.CharField(max_length=200)
status = models.CharField(choices=STATUS, max_length=30)
remove comma from first line
STATUS = (('active', 'active'), ('inactive', 'inactive')),
This line should be converted to this
STATUS = (('active', 'active'), ('inactive', 'inactive'))
I'm having a problem with some models in django. I want to set up 2 Tables ( Table and Order ) like this:
Code
class Table(models.Model):
number = models.PositiveSmallIntegerField()
restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
current_order = models.OneToOneField(Order, blank=True)
is_free = models.BooleanField(default=True)
def __str__(self):
return "Table " + str(self.number) + " - Restaurant " + str(self.restaurant)
class Order(models.Model):
table = models.ForeignKey(Table, on_delete=models.CASCADE)
orders = models.ManyToManyField(Dish, through='DishOrder')
def __str__(self):
return "Order " + self.id
Error
When tryng to makemigrations django block me because 'Order is not definied'.
I understand the problem but I can't move the Order class before Table class otherwise the error will be 'Table not definied'
How can I solve this?
I'm using Wagtail, and I have two models that have some fields in common. I would like to get a query of the union of those common fields. Here are the models:
class VideoPage(Page):
image = models.ForeignKey("wagtailimages.Image", blank=False, null=True, related_name="+", on_delete=models.SET_NULL)
vimeo_id = models.CharField(max_length=25)
publish_date = models.DateField(null=False, blank=False, default=datetime.date.today)
category_name = models.CharField(max_length=100, null=False, blank=True, default="Videos")
pdf = models.ForeignKey('wagtaildocs.Document', null=True, blank=False, default=1, on_delete=models.SET_NULL, related_name='+')
class CustomerHubFile(models.Model):
CASESTUDY = 'Case Study'
DATASHEET = 'Data Sheet'
SOLUTIONBRIEF = 'Solution Brief'
CATEGORY = [
(CASESTUDY, _('Case Study')),
(DATASHEET, _('Data Sheet')),
(SOLUTIONBRIEF, _('Solution Brief')),
]
title = models.CharField(max_length=255)
pdf = models.ForeignKey('wagtaildocs.Document', null=True, blank=False, on_delete=models.SET_NULL, related_name='+')
image = models.ForeignKey("wagtailimages.Image", blank=False, null=True, related_name="+", on_delete=models.SET_NULL, help_text="The image for the PDF")
publish_date = models.DateField(null=False, blank=False)
category_name = models.CharField(max_length=100, null=False, blank=True, choices=CATEGORY, default=CASESTUDY, verbose_name="Category")
Then I'm trying to add a query that joins both of these models together and return it in the context. I can't use values() or value_list() because I need access to the models in the template in order to use the images. I'd think I should be able to do it using only() like this:
pdfs = CustomerHubFile.objects.all().only('title', 'image', 'publish_date', 'category_name')
videos = VideoPage.objects.all().only('title', 'image', 'publish_date', 'category_name')
records = pdfs.union(videos).order_by('-publish_date')
context["records"] = records
return context
Unfortunately, only() also gets the id, but it doesn't get them in the same order for both models, which messes up the JOIN, resulting in an error that says "UNION types integer and character varying cannot be matched". Here is the generated SQL:
(
SELECT "customer_hub_customerhubfile"."id",
"customer_hub_customerhubfile"."title",
"customer_hub_customerhubfile"."image_id",
"customer_hub_customerhubfile"."publish_date",
"customer_hub_customerhubfile"."category_name"
FROM "customer_hub_customerhubfile"
ORDER BY "customer_hub_customerhubfile"."publish_date" DESC
)
UNION (
SELECT "wagtailcore_page"."title",
"customer_hub_videopage"."page_ptr_id",
"customer_hub_videopage"."image_id",
"customer_hub_videopage"."publish_date",
"customer_hub_videopage"."category_name"
FROM "customer_hub_videopage"
INNER JOIN "wagtailcore_page"
ON ("customer_hub_videopage"."page_ptr_id" = "wagtailcore_page"."id")
ORDER BY "wagtailcore_page"."path" ASC
)
I tried specifying the id fields in the only() but it still doesn't get them in specified order. Is there any way to get the ids to return as the first column for both queries, or is there another way I can get this union to work?
I'm getting the error message:
insert or update on table "quizzer_progress" violates foreign key
constraint. DETAIL: Key (word_id)=(4700) is not present in table
"quizzer_alone_words".
but it makes no sense.
I have these 3 tables in models.py:
class Progress(models.Model):
success = models.IntegerField(default=0)
fail = models.IntegerField(default=0)
total = models.IntegerField(default=0)
word = models.ForeignKey(Word, related_name='word_progress', blank=True, null=True)
class WordLesson(models.Model):
text = models.CharField(max_length=3000, blank=True)
owner = models.ForeignKey(User, related_name='Words_from_lessons')
words = models.ManyToManyField('Alone_Words', blank=True)
min_age = models.IntegerField(default=0)
max_age = models.IntegerField(default=0)
class Alone_Words(models.Model):
lesson = models.ForeignKey(WordLesson, related_name='lesson_of_the_word', blank=True, default=1)
word = models.CharField(max_length=100, blank=True)
position = models.IntegerField(default=0)
As you can see, the table where I'm trying to save data has nothing to do with the one that "violates foreign key constraint"
Anyone can tell me what's happening????
NOTE: the code failing is this one ->
p = Progress(success =0,fail = 0,total =0,word = myword)
p.save()