Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an app with several django/python data models and associated postgres database tables. In this case the important ones are User and Feed.

I need to create a one to one relationship between a column in my Feed model and items in my User model. If I were starting the app from scratch I would simply define a user property in my Feed models as such:

from django.contrib.auth.models import User

class Feed(models.Model):
    ...
    user = models.OneToOneField(User)

Of course, being that my app is already functioning, I need to modify my database tables with SQL commands in accordance with my django/python data model changes.

My question is, what SQL commands do need to create a OneToOneField? I've used the documentation here: http://www.djangobook.com/en/2.0/chapter10.html as a guide when doing SQL commands in the past. But I'm concerned that I'm gonna screw something up when it comes to OneToOneFields.

The django manage.py sqlall command tells me that I need a column that looks like:

"user_id" integer NOT NULL UNIQUE REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED
share|improve this question
    
Why not use a migration app? –  Ignacio Vazquez-Abrams Jun 25 '13 at 3:16
    
That of course requires me to configure the library and learn their API. I'm lazy, I want a one-time SQL command. But based on the lackluster response to this question, I'm thinking I might have to learn a migration app. Ive heard south is good. –  eblahm Jun 26 '13 at 14:54

1 Answer 1

up vote 0 down vote accepted

For all you out there with a similar problem... I ended up opting for the south migration library. (at some point I'd still like to learn SQL commands). Getting south to work was fairly straightforward.

$ pip install south
$ cd [pathtoproject]
$ vim [project]/settings.py

# make sure the following is reflected
INSTALLED_APPS += ('south',) 

$ ./manage.py syncdb
$ ./manage.py convert_to_south [myapp]
$ vim [myapp]/models.py

# make the changes to the data models. 
# south has to be initialized before these changes occur

class Feed(models.Model):
    ...
    user = models.OneToOneField(User)


$ ./manage.py schemamigration [myapp] --auto
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.