2👍
✅
The answer you are looking for I think would lie in the SQL schema that you altered and not in the django models.
It could probably have something to do with null
or blank values in the news_category_id
, or news that belongs to a category that doesn’t exist in the news_category. Things I’d check:
- You have renamed the primary key on the News category from
news_category_id
toid
. Does the foreign key on the News also map tonews_category_id
and not anything else? - Are all the values captured in the
news.news_category
also present innews_category.id
Also, as an aside, I don’t see any reason why you need to rename the primary keys to id
from something that they already are. Just marking them primary_key=True
works just fine. Django provides you a convenient alias pk
to access a model’s integer primary key, irrespective of what the name of the field actually is.
Source:stackexchange.com