views:

131

answers:

1

I have a model called SimplePage in which I have this line:

category = models.ForeignKey('Category', related_name='items',
                             blank=True, null=True)

I assumed this will allow me to have SimplePage instances that do not have a Category.

But for some reason, when I try to create a SimplePage in the Admin with no Category, I get:

IntegrityError at /admin/sitehelpers/simplepage/add/
sitehelpers_simplepage.category_id may not be NULL

What is this?

+3  A: 

Could it possibly be that you added the null=True attribute after doing the syncdb for that model? Django won't change database tables, only create them. Check in your database if NULL is allowed for that column and change it manually.

piquadrat
Remember kids: Django will only CREATE tables or columns if they don't exist. It will never ALTER a table for you. Any table changes necessary to reflect changes to models will have to be done manually by you!
jathanism
...or you can start using third-party schema migration assistant, like South.
Ludwik Trammer
I actually ran syncdb and it didn't help. What worked was deleting the db and then doing syncdb.
cool-RR
@cool-RR, synack's comment about syncdb creating but not modifying tables is what bit you.
istruble
I expected syncdb to give a warning about this.
cool-RR
this is clearly documented behavior: http://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb. If you don't bother to read the documentation, you're bound to run into a wall once in a while.
piquadrat
Does it say there that syncdb will not give a warning?
cool-RR