Django没有这样的表:

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (22)

我在Django的模型如下所示。

class Pupil(models.Model):
    forename = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    dateofbirth = models.DateField()
    year = models.IntegerField()
    class_group = models.CharField(max_length=30)
    email = models.EmailField(blank=True)
    assignments = models.ManyToManyField('Assignment', verbose_name='related assignments')

    def __unicode__(self):
        return u'%s %s' % (self.forename, self.surname)

class Subject(models.Model):
    name = models.CharField(max_length=30)
    level = models.CharField(max_length=30)
    teachers = models.ManyToManyField('Teacher', verbose_name='related teachers')

    def __unicode__(self):
        return self.name

class Teacher(models.Model):
    forename = models.CharField(max_length=30)
    surname = models.CharField(max_length=30)
    email = models.EmailField(blank=True)

    def __unicode__(self):
        return u'%s %s' % (self.forename, self.surname)

class Assignment(models.Model):
    assignment_name = models.CharField(max_length=30)
    date_assigned = models.DateField()
    date_submitted = models.DateField()

    def __unicode__(self):
        return self.assignment_name

当我试图添加并将一个任务附加到管理员中时,我会得到一个数据库错误-

no such table: homework_pupil_assignments

我意识到这可能是因为Django没有像我那样更新我的模型。manage.py sqlall homework

我看到以下情况:

BEGIN;
CREATE TABLE "homework_pupil_assignments" (
    "id" integer NOT NULL PRIMARY KEY,
    "pupil_id" integer NOT NULL,
    "assignment_id" integer NOT NULL,
    UNIQUE ("pupil_id", "assignment_id")
)
;
CREATE TABLE "homework_pupil" (
    "id" integer NOT NULL PRIMARY KEY,
    "forename" varchar(30) NOT NULL,
    "surname" varchar(30) NOT NULL,
    "dateofbirth" date NOT NULL,
    "year" integer NOT NULL,
    "class_group" varchar(30) NOT NULL,
    "email" varchar(75) NOT NULL
)
;
CREATE TABLE "homework_subject_teachers" (
    "id" integer NOT NULL PRIMARY KEY,
    "subject_id" integer NOT NULL,
    "teacher_id" integer NOT NULL,
    UNIQUE ("subject_id", "teacher_id")
)
;
CREATE TABLE "homework_subject" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(30) NOT NULL,
    "level" varchar(30) NOT NULL
)
;
CREATE TABLE "homework_teacher" (
    "id" integer NOT NULL PRIMARY KEY,
    "forename" varchar(30) NOT NULL,
    "surname" varchar(30) NOT NULL,
    "email" varchar(75) NOT NULL
)
;
CREATE TABLE "homework_assignment" (
    "id" integer NOT NULL PRIMARY KEY,
    "assignment_name" varchar(30) NOT NULL,
    "date_assigned" date NOT NULL,
    "date_submitted" date NOT NULL
)
;
COMMIT;

然后,我安装了Southern,按照使用现有应用程序运行的指南,希望能成功地同步这些表。

有人能建议我如何让数据库(Sqlite 3)反映模型,或者指出我做错了什么吗?

提问于
用户回答回答于

完成这个检查列表(我假设您不能删除sqlite 3数据库文件来重新开始):

  1. 获取任何SQLite GUI工具(即http://sqliteadmin.robmu2k.de/)
  2. 更改模型定义以与数据库定义匹配(最佳方法是注释新字段)
  3. 删除migrations模型中的文件夹
  4. 删除行south_migrationhistoryapp_name与您的应用程序名称匹配(可能)homework)
  5. 援引:./manage.py schemamigration <app_name> --initial
  6. 创建表./manage.py migrate <app_name> --fake(--fake将跳过SQL执行,因为数据库中已经存在表)
  7. 更改应用程序的模型
  8. 调用./manage.py schemamigration <app_name> --auto
  9. 然后将更改应用于数据库:./manage.py migrate <app_name>

步骤7、8、9每当模型需要任何更改时都会重复。

用户回答回答于

你可以试试这个!

python manage.py migrate --run-syncdb

我对Django 1.9和1.10也有同样的问题。这个密码有效!

扫码关注云+社区