ForeignKey pointing to "nothing"

17 views
Skip to first unread message

Phil Hughes

unread,
Jan 3, 2014, 8:57:13 PM1/3/14
to django...@googlegroups.com
I have a model where a foreign key reference may be undefined when a
record is first created. In particular, I have the following in my model
 seller = models.ForeignKey(User, related_name='+', blank=True,
     null=True)

The form accepts not setting the seller field but I get an Integrity
Error exception when I attempt to save it that says
    escrow_escrow.seller_id may not be NULL

I am using SQLite. What did I miss?

Russell Keith-Magee

unread,
Jan 4, 2014, 5:26:59 AM1/4/14
to Django Users
Was the null=True restriction in place when you created the table? If you ran syncdb and *then* added the null=True definition, the null=True definition won't be picked up by the  database - you'll need to do a table migration. However, it *will* be picked up by forms, as they read the model definition as-is.

Migrating tables is a bit painful on SQLite; it will probably be easier to just drop the table and recreate it. If you need to preserve data in the table, you can use the dumpdata/loaddata management commands.

Yours,
Russ Magee %-)

Phil Hughes

unread,
Jan 4, 2014, 8:23:06 AM1/4/14
to django...@googlegroups.com
That was it -- thanks. I usually create a MySQL database to use in development but decided to just use sqlite. So much for my "shortcut".
Reply all
Reply to author
Forward
0 new messages