【发布时间】:2017-06-02 03:35:40
【问题描述】:
我正在使用 Django 1.9 运行 Web 应用程序,并且有许多模型。 (可能太长了)
老款:
class Application_Delete_History(models.Model):
name = models.CharField(max_length=128)
delete_time = models.DateTimeField(null=True)
status = models.BooleanField(default=True)
class Review_Manager(models.Model):
name = models.CharField(max_length=128)
display_name = models.CharField(max_length=128)
我在 2 个模型中添加了许多数据。 通过我的愚蠢行为,我创建了 2 个新模型:
class Application_Status_Notification(models.Model):
name = models.CharField(max_length=128,null=True,blank=True)
created = models.DateTimeField(null=True,blank=True)
last_use = models.DateTimeField(null=True,blank=True, default=None)
And the worst, I edit the old one:
class Application_Delete_History(models.Model):
name = models.CharField(max_length=128)
delete_time = models.DateTimeField(null=True)
status = models.BooleanField(default=True)
# Add:
is_fa = models.NullBooleanField(null=True, blank=True)
is_ide = models.NullBooleanField(null=True, blank=True)
applicant = models.CharField(max_length=64, blank=True) # with out : null=True,
defect_manager = models.ForeignKey(Review_Manager,null=True, default=None)
admin_comment = models.CharField(max_length=256, null=True, blank=True)
然后运行:python3.4 manage.py migrate(我做了 makemigrations 和 sqlmigrate)
And the ERROR show up: ForeignKey can't be fill (The data allready exit conflix with it) ~> The models change but Migare FAIL.
现在我想解决这个问题,但是: 当我删除新迁移并创建一个新迁移时,1 得到:
我确定我编辑过
defect_manager = models.ForeignKey(Review_Manager,null=True, default=None)
还有:
File "/usr/local/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (1060, "Duplicate column name 'admin_comment'")
这个问题在 Django 1.11 中不会发生(我正在测试它并且可以编辑数据,因为:Migrate 会将退出的重命名为 model_old 并将其映射到更新一个)
BEGIN;
--
-- Add field defect_manager to application_delete_history
--
ALTER TABLE "table_application_delete_history" RENAME TO "table_application_dele
te_history__old";
CREATE TABLE "table_application_delete_history" ("id" integer NOT NULL PRIMARY K
EY AUTOINCREMENT, "defect_manager_id" integer NULL REFERENCES "table_review_mana
ger" ("id"), "name" varchar(128) NOT NULL, "delete_time" datetime NULL, "status"
bool NOT NULL, "admin_comment" varchar(256) NULL, "applicant" varchar(64) NULL,
"is_fa" bool NULL, "is_ide" bool NULL);
INSERT INTO "table_application_delete_history" ("id", "name", "delete_time", "st
atus", "admin_comment", "applicant", "is_fa", "is_ide", "defect_manager_id") SEL
ECT "id", "name", "delete_time", "status", "admin_comment", "applicant", "is_fa"
, "is_ide", NULL FROM "table_application_delete_history__old";
DROP TABLE "table_application_delete_history__old";
CREATE INDEX "table_application_delete_history_defect_manager_id_d577266a" ON "t
able_application_delete_history" ("defect_manager_id");
COMMIT;
但 DJANGO 1.9 没有! 我怎么解决这个问题??? 我放弃了新的创建模型 ~> 可以创建新模型但不能编辑旧模型。
编辑 1: 我可以通过以下方式修复它:删除所有新的 COLUMN 并再次迁移。 但它不禁让人感觉良好......
【问题讨论】:
-
看看
python manage.py migrate --fake -
谢谢,我试试看。
标签: django django-models django-migrations