Django - syncdb不创建表 [英] Django - syncdb doesn't create tables

查看:93
本文介绍了Django - syncdb不创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个多对多的字段添加到现有的模型中,并期待 syncdb 创建一个新的表,但没有任何内容。这是模型的样子:

I added a many-to-many field to an existing model and was expecting syncdb to create a new table, but there's nothing there. This is what the model looks like:

class Author(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()

    def __unicode__(self):
        return "{0} {1}".format(self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)

    def __unicode__(self):
        return self.title

运行 sql myapp 使用新表打印正确的语句,但是当我运行 syncdb时,这并不反映 validate 也没有返回任何错误。有谁知道这可能是什么事?或更好的诊断?

Running sql myapp prints the correct statements with the new table, but this is not reflected when I run syncdb. validate also returns no errors. Does anyone know what could be the matter here? Or a better diagnostic?

推荐答案

syncdb命令不会通过设计为现有型号创建多个到多个表。这个决定是在 ticket 2229 上解释的。

The syncdb command does not create many to many tables for existing models by design. This decision is explained on ticket 2229.

让你有几个选项。


  • 如果您的图书模型中没有任何数据,请删除表并重新运行syncdb。 Django将重新创建书桌和许多表格。

  • 使用dbshel​​l命令,并使用sql myapp的输出创建多个到多个加入表。

  • 如果您正在使用Django进行多个架构迁移,请与南部进行交流。

  • If you don't have any data in your Book model, drop the table and rerun syncdb. Django will recreate the book table and the many to many table.
  • Use the dbshell command, and create the many to many joining table using the output of sql myapp.
  • If you're doing multiple schema migrations with Django, make friends with South.

这篇关于Django - syncdb不创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆