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):
datet开发者_开发问答ime = 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)
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.
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.
精彩评论