views:

33

answers:

4

Hi, In my models I want to have an optional field to a foreign key. I tried this:

field = models.ForeignKey(MyModel, null=True, blank=True, default=None)

But i am getting this error:

model.mymodel_id may not be NULL

i am using sqlite edit: if it can help, here is the exception location:

/usr/lib/python2.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 200

so it's sqlite specific problem, i think.

+2  A: 

If it was previously not null and you synced it before then resyncing won't change it. Either drop the table, use a migration tool such as South, or alter the column in SQL directly.

Ignacio Vazquez-Abrams
i removed sqlite database file, and synced, but i still have the same problem. :(
maroxe
A: 

I believe that it has to be both Null-True and Blank=True.

ablerman
That's exactly what i did
maroxe
.. which, in the code snippet in question, is the case ...
The MYYN
A: 

I'm not sure on this one, but I think you need to lose the "default=None" and do a reset/syncdb. The default overrides the null/blank info. I.e. if you give a blank in the admin it will store None (whose representation may vary from DB to DB). I would need to look at the code/docs to be more certain.

Peter Rowell
I don't know if losing the default=None will fix this but I think you should remove it either way. (Could be wrong though.)
Andrew Hubbs