views:

365

answers:

2

Hi there,

I need to add a foreign key field to an existing django model/postgres table. As per the django documentation, I ran the 'sqlall myapp' command to 'work out the difference'.

The obvious difference is that the table in question now has an extra column with a new contraint, which looks like this:

ALTER TABLE "myapp_mytable" ADD CONSTRAINT newcolumn_id_refs_id_4bfb2ece 
FOREIGN KEY ("newcolumn_id") REFERENCES "myapp_theothertable" ("id") 
DEFERRABLE INITIALLY DEFERRED;

Before messing with my database, I'd like to understand that statement, in particular, what does the last part of newcolumn_id_refs_id_4bfb2ece refer to?

Thanks,

Martin

A: 

It will make PostgreSQL understand and enforce your foreign key to the other table and guarantee there won't be anything in myapp_table.newcolumn that can't be found in myapp_theothertable.id

Actually, your django app will work just fine even without that constraint. However, it's a good idea to have one in place, and if you do afterwards a dumpdata - loaddata -cycle, it will be created.

af
A: 

It's probably a uniquess hash to keep the constraint name under a certain length. I don't see a reference to this in the context of constraints, but they describe the process for foreign key column naming here: http://docs.djangoproject.com/en/dev/ref/models/fields/#id1.

marcopolo1010