Shehzad009
Shehzad009

Reputation: 1607

Want to add a one-to-one column to an existing table

I have a new field in the Client called note which has a one-to-one relation with Notes. I want to be able to add the note column under the Client table in mysql. It does not work when I use python manage.py syncdb. So I would like to know how to add a one-to-one field in mysql to an existing table.

models.py

class Note(models.Model):
    datetime = models.DateTimeField(default=datetime.now)
    user  = models.ForeignKey(User)
    note = models.TextField()

    def __unicode__(self):
        return unicode(self.name)

class Client(models.Model):
    note = models.OneToOneField(Note) # new
    def __unicode__(self)
       return unicode(self.user)

Upvotes: 1

Views: 423

Answers (2)

Krab
Krab

Reputation: 2148

Syncdb doesn't change your database in order not to destroy your data. See django book.

You can update the database manually or use an automated migration tool (at your own risk) like South.

EDIT: In django book, you can also read detailed instructions about manual changes along with SQL examples. Other useful information can be found in MySQL manual.

Upvotes: 1

Gert Steyn
Gert Steyn

Reputation: 2204

You didn't say what the application name is so I'll assume it is fooapp.

First look at the SQL Django would have used to create the relevant tables:

python manage.py sql fooapp

For additional fields run this sql command:

ALTER TABLE fooapp_client ADD ...

where ... is the field declaration you saw in the output of "manage.py sql fooapp"

Remember to also execute any index/constraint commands in the "manage.py sql fooapp" output that operate on the new field.

Upvotes: 0

Related Questions