Django:v1.9 迁移现有模型错误
Django:v1.9 migrate the exiting model ERROR
我正在 运行使用 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 迁移(我做了 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.
现在我想解决这个问题但是:
当我删除新的迁移并创建一个新的迁移时,我得到了:
我确定我编辑了
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
如果上述解决方案不起作用,请逐步执行此操作:-
1.Empty the django_migrations table: delete from django_migrations;
2.For every app, delete its migrations folder: rm -rf <app>/migrations/
3.Reset the migrations for the "built-in" apps: python manage.py migrate --fake
4.For each app run: python manage.py makemigrations <app>. Take care of dependencies (models with ForeignKey's should run after their parent model).
5.Finally: python manage.py migrate --fake
我正在 运行使用 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 迁移(我做了 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.
现在我想解决这个问题但是: 当我删除新的迁移并创建一个新的迁移时,我得到了:
我确定我编辑了
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
如果上述解决方案不起作用,请逐步执行此操作:-
1.Empty the django_migrations table: delete from django_migrations;
2.For every app, delete its migrations folder: rm -rf <app>/migrations/
3.Reset the migrations for the "built-in" apps: python manage.py migrate --fake
4.For each app run: python manage.py makemigrations <app>. Take care of dependencies (models with ForeignKey's should run after their parent model).
5.Finally: python manage.py migrate --fake