gpt4 book ai didi

django 没有这样的表 :

转载 作者:行者123 更新时间:2023-11-28 19:34:45 25 4
gpt4 key购买 nike

我在 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

看完this我意识到这可能是因为 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;

然后我安装了 South,按照说明启动并使用现有应用程序运行,希望能够成功同步这些表格。没有快乐。

谁能建议我如何获取数据库 (sqlite3) 来反射(reflect)模型或指出我做错了什么?

最佳答案

没有 south 的 Django 迁移的更新答案插件:

T.T建议在他的answer ,我之前的回答是 south迁移插件,当 Django 没有任何架构迁移功能时。现在(适用于 Django 1.9+):

T.T wrote :

You can try this!

python manage.py makemigrations

python manage.py migrate --run-syncdb

已过期south迁移插件

As I can see you done it all in wrong order, to fix it up your should complete this checklist (I assume you can't delete sqlite3 database file to start over):

  1. Grab any SQLite GUI tool (i.e. http://sqliteadmin.orbmu2k.de/)
  2. Change your model definition to match database definition (best approach is to comment new fields)
  3. Delete migrations folder in your model
  4. Delete rows in south_migrationhistory table where app_name match your application name (probably homework)
  5. Invoke: ./manage.py schemamigration <app_name> --initial
  6. Create tables by ./manage.py migrate <app_name> --fake (--fake will skip SQL execute because table already exists in your database)
  7. Make changes to your app's model
  8. Invoke ./manage.py schemamigration <app_name> --auto
  9. Then apply changes to database: ./manage.py migrate <app_name>

Steps 7,8,9 repeat whenever your model needs any changes.

关于django 没有这样的表 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12784835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com