Django 1.8:为现有架构创建初始迁移 [英] Django 1.8: Create initial migrations for existing schema
问题描述
我开始了一个django 1.8项目,它使用迁移系统。
不知何故,事情变得凌乱,所以我从DB中删除了迁移文件夹和表,现在我试图重建它们,没有成功。
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:
- 擦除所有
迁移
文件夹。完成! - 从
django_migrations
表中删除所有内容。完成! - 为每个应用程序运行
python manage.py makemigrations --empty< app>
完成! - 运行
python manage.py migrate --fake
。完成! (虽然它只有在每个makemigrations
命令之后运行时才起作用。
- Erase all
migrations
folders. Done! - Delete everything from the
django_migrations
table. Done! - Run
python manage.py makemigrations --empty <app>
for every app. Done! - Run
python manage.py migrate --fake
. Done! (although it works only if I run it after everymakemigrations
command.
现在我添加一个新的字段,运行 makemigrations
命令,我收到以下错误:
django.db.utils .OperationalError:(1054,'未知列'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'")
我一直在燃烧HOURS这个东西,我可以如何初始化迁移,以便每次都可以继续工作,而不会有迁移中断?
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
?
编辑:
现在事情变得平衡nastier,我截断了 django_migrations
表,并删除了所有的迁移
文件夹。
现在我尝试运行 python manage.py migrate --fake-initial
(我在DEV文档中找到的东西),只需设置Django的所有内部应用程序(auth,session等),我得到:
(1054,'django_content_type'中的未知列'名称)
。 br>
现在,这个列不是一个真正的列。在Django的 contenttypes
应用程序中定义了一个 @property
。这里发生了什么?为什么将名称
属性识别为真正的列?
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):
- 清空
django_migrations
表: code>从django_migrations删除; - 对于每个应用程序,删除其
迁移
文件夹: code> rm -rf< app> / migrations / - 重置内置应用的迁移:
python manage.py migrate --fake
- 对于每个运行的应用程序:
python manage.py makemigrations< app>
。最后:python manage.py migrate --fake-initial $ c $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>
- Empty the
django_migrations
table:delete from django_migrations;
- For every app, delete its
migrations
folder:rm -rf <app>/migrations/
- Reset the migrations for the "built-in" apps:
python manage.py migrate --fake
- For each app run:
python manage.py makemigrations <app>
. Take care of dependencies (models with ForeignKey's should run after their parent model). - 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
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屋!