Django Model syncdb创建现有表

时间:2012-05-10 10:40:49

标签: django django-models django-syncdb

在django模型中,我有一个名为Invitation的类。

我执行manage.py syncdbInvitation已创建。 这是我的第一张表。

但是当我创建另一个名为Image的类并执行manage.py syncdb

它会返回错误。

Table UserInvitation already exists

请帮我使用syncdb命令创建表Image

模型

 class UserInvitation(models.Model):

   InvitationID =  models.AutoField(primary_key=True)
   InvitationCode =  models.CharField(max_length=255)
   Email = models.CharField(max_length=150)
   ExpireDate = models.DateTimeField()
   Deleted = models.BooleanField(default=False)
   CreatedDate =  models.DateTimeField(auto_now=True)

 class Meta:
    db_table = u'UserInvitation' 

class UserImage(models.Model):
   ImageID =  models.AutoField(primary_key=True)
   UserID = models.IntegerField(unique=True)
   FileName =  models.CharField(max_length=255)

class Meta:
    db_table = u'UserImage'

1 个答案:

答案 0 :(得分:0)

您应该使用来反映数据库中的更改。

syncdb运行创建数据库中所有表的命令,而不仅仅是您添加的表。

对于South,您应该将模型恢复为仅具有UserInvitation类的模式,运行命令:

python manage.py convert_to_south <name of app>

添加UserImage类并运行命令:

python manage.py schemamigration --auto <name of app>
python manage.py migrate <name of app>

这会在数据库中添加userimage表。

此外,元类中的db_table字段应为小写,因为表名在创建时为小写(请检查数据库以确保)

请参阅南方文档:http://south.readthedocs.org/en/latest/

相关问题