Django 1.8:为现有模式创建初始迁移 [英] Django 1.8: Create initial migrations for existing schema

查看:341
本文介绍了Django 1.8:为现有模式创建初始迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我启动了一个使用迁移系统的 django 1.8 项目.
不知何故,事情变得一团糟,所以我从数据库中删除了迁移文件夹和表,现在我试图重建它们,但没有成功.

I started a django 1.8 project, which uses the migrations system.
Somehow along the way things got messy, so I erased the migrations folders and table from the DB, and now I'm trying to reconstruct them, with no success.

我有三个应用程序(3 个 models.py 文件),模型完全反映了表格!

I have three apps (3 models.py files), and the models reflect the tables EXACTLY!

到目前为止,我发现的最佳方法是:

The best approach that I've found so far was:

  1. 清除所有 migrations 文件夹.完成!
  2. django_migrations 表中删除所有内容.完成!
  3. 为每个应用运行 python manage.py makemigrations --empty <app>.完成!
  4. 运行 python manage.py migrate --fake.完毕!(尽管它只有在我在每个 makemigrations 命令之后运行它时才有效.
  1. Erase all migrations folders. Done!
  2. Delete everything from the django_migrations table. Done!
  3. Run python manage.py makemigrations --empty <app> for every app. Done!
  4. Run python manage.py migrate --fake. Done! (although it works only if I run it after every makemigrations command.

现在我添加一个新字段,运行 makemigrations 命令,我收到以下错误:
django.db.utils.OperationalError: (1054, "Unknown column 'accounts_plan.max_item_size' in 'field list'")

Now I add a new field, run the makemigrations command, and I receive the following error:
django.db.utils.OperationalError: (1054, "Unknown column 'accounts_plan.max_item_size' in 'field list'")

我在这件事上花了好几个小时.我如何初始化迁移,以便每次都可以继续工作而不会中断迁移?

I've been burning HOURS on this thing. How the h**l can I initialize the migrations so I can continue working without migration interruptions every time?

为什么这么复杂?为什么没有简单的单行代码:initiate_migrations_from_schema?

Why is it so complicated? Why isn't there a simple one-liner: initiate_migrations_from_schema?


现在事情变得更糟了.我截断了 django_migrations 表并删除了所有 migrations 文件夹.
现在我尝试运行 python manage.py migrate --fake-initial (我在 DEV 文档中找到的东西),只是为了设置 Django 的所有内部"应用程序(身份验证、会话等)) 我得到:
(1054, "'django_content_type' 中的未知列 'name'").
现在,这个列"不是真正的列.它是在 Django 的 contenttypes 应用程序中定义的 @property.这里发生了什么?为什么将 name 属性标识为真实列?


Now things get even nastier. I truncated the django_migrations table and deleted all the migrations folder.
Now I try to run python manage.py migrate --fake-initial (something I found in the DEV docs), just so it sets up all of Django's 'internal' apps (auth, session, etc) and I'm getting:
(1054, "Unknown column 'name' in 'django_content_type'").
Now, this "column" is not a real column. It's a @property defined in Django's contenttypes app. WHAT IS GOING ON HERE? Why is it identifying the name property as a real column?

推荐答案

终于搞定了,虽然不知道为什么,希望以后能用.
经过多次试验并浏览了 Django 的开发站点 (链接).
以下是步骤(适用于遇到此问题的人):

Finally got it to work, although I don't know why and I hope it will work in the future.
After doing numerous trials and going through Django's dev site (link).
Here are the steps (for whoever runs into this problem):

  1. 清空 django_migrations 表:delete from django_migrations;
  2. 对于每个应用,删除其 migrations 文件夹:rm -rf <app>/migrations/
  3. 重置内置"应用的迁移:python manage.py migrate --fake
  4. 对于每个应用运行:python manage.py makemigrations <app>.注意依赖关系(具有 ForeignKey 的模型应该在其父模型之后运行).
  5. 最后:python manage.py migrate --fake-initial
  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-initial

之后我运行了最后一个没有 --fake-initial 标志的命令,只是为了确保.

After that I ran the last command without the --fake-initial flag, just to make sure.

现在一切正常,我可以正常使用迁移系统.

Now everything works and I can use the migrations system normally.

我确定我不是唯一遇到此问题的人.必须更好地记录,甚至简化.

I'm sure I'm not the only one who encounters this issue. It must be documented better and even simplified.

Django 1.9 用户更新:
我在 Django 1.9.4 中再次遇到了这种情况,第 5 步失败了.
我所要做的就是将 --fake-initial 替换为 --fake 以使其工作.

Update for Django 1.9 users:
I had this scenario again with a Django 1.9.4, and step 5 failed.
All I had to do is replace --fake-initial with --fake to make it work.

这篇关于Django 1.8:为现有模式创建初始迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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