Python Django 1.8 migrate 不创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33086444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django 1.8 migrate is not creating tables
提问by YSK
yekabathula-macbookair2:roster yekabathula$ python manage.py migrate
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, api, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying api.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying sessions.0001_initial... OK
yekabathula-macbookair2:roster yekabathula$ python manage.py syncdb
/Library/Python/2.7/site-packages/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, api, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
No migrations to apply.
After doing python manage.py migrate, tables are not created in database from my models.py it is able to create other tables from django_session etc. Is there anything else that I need to follow here ?
执行 python manage.py migrate 后,表不会从我的 models.py 的数据库中创建它能够从 django_session 等创建其他表。还有什么我需要在这里遵循的吗?
回答by user3130740
I had a similar problem and just figured it out.I have multiple databases. My local one (the one not being updated) is a MySQL database. The others are MS SQL Server and MySQL. I have routers to the other databases since I do not manage them and had (in Django 1.6) used the routers to indicate allow_sync() = False. With 1.7, I changed that to allow_migrate() = False. BUT I DID NOT ADD A ROUTER FOR MY LOCAL DATABASE. The default appears to be allow_migrate() = False if there is not one. As a result, the migrations just failed silently ( Reference: https://docs.djangoproject.com/en/1.7/topics/db/multi-db/). I added a router for my local DB, setting allow_migrate() to return True and now my migrations actually create my tables.
我有一个类似的问题,只是想通了。我有多个数据库。我的本地(未更新的)是一个 MySQL 数据库。其他的是 MS SQL Server 和 MySQL。我有到其他数据库的路由器,因为我不管理它们并且(在 Django 1.6 中)使用路由器来指示 allow_sync() = False。在 1.7 中,我将其更改为 allow_migrate() = False。但我没有为本地数据库添加路由器。如果没有,默认值似乎是 allow_migrate() = False 。结果,迁移只是默默地失败了(参考:https: //docs.djangoproject.com/en/1.7/topics/db/multi-db/)。我为本地数据库添加了一个路由器,将 allow_migrate() 设置为返回 True,现在我的迁移实际上创建了我的表。
回答by Sна?ош?а?
This solved the problem for me (I am using MySQL workbench by the way):
这为我解决了问题(顺便说一下,我正在使用 MySQL 工作台):
- Run this sql:
SET FOREIGN_KEY_CHECKS = 0;
- Select all the tables in your django database(click on the first table, then press and hold shift, then click on the last table). Then right click and choose "Drop n tables" (where n is the number of tables you just selected)
- then run
python manage.py migrate
- Finally restore foreign key check settings by running this sql:
SET FOREIGN_KEY_CHECKS = 1;
- 运行这个sql:
SET FOREIGN_KEY_CHECKS = 0;
- 选择django 数据库中的所有表(单击第一个表,然后按住 shift,然后单击最后一个表)。然后右键单击并选择“删除 n 个表”(其中 n 是您刚刚选择的表数)
- 然后运行
python manage.py migrate
- 最后通过运行此 sql 来恢复外键检查设置:
SET FOREIGN_KEY_CHECKS = 1;
Note: Before taking this drastic measure, I tried what Paulo Pessoasaid in his comment, but still I got "No migrations to apply." messages. However, this solved the issue.
注意:在采取这种严厉措施之前,我尝试了Paulo Pessoa在他的评论中所说的,但我仍然得到“没有可申请的迁移”。消息。然而,这解决了这个问题。
回答by bhaskarc
I was facing a similar problem in Django 1.10 and none of the above solutions worked for me.
我在 Django 1.10 中遇到了类似的问题,上述解决方案都不适合我。
What eventually worked was running this command:
最终奏效的是运行这个命令:
python manage.py migrate --fake myappname zero
This reset all migrations (to the zeroth state)
这将重置所有迁移(到第零个状态)
This followed by :
接下来是:
python manage.py migrate myappname
created the tables for me.
为我创建了表格。
If you do not want to roll back to the initial(zero) state but say to the migration number 0005(the last migration that worked), you can instead do this:
如果您不想回滚到初始(零)状态,而是说迁移编号 0005(最后一次成功迁移),您可以改为执行以下操作:
python manage.py migrate --fake myappname 0005
And then proceed with the actual migrate:
然后继续实际的迁移:
python manage.py migrate myappname
More details in the docs
文档中的更多详细信息
回答by Robin Wu Yu
Delete existed tables of the models.
Delete migration folder under app folder.
Delete all related migration records in the table
"django_migrations".Now you get clear model and database. Use makemigrations and migrate to create table.
删除模型的现有表。
删除 app 文件夹下的迁移文件夹。
删除
“django_migrations”表中所有相关的迁移记录。现在你得到了清晰的模型和数据库。使用 makemigrations 和 migrate 创建表。
Hope to help you.
希望能帮到你。
回答by Vkreddy Komatireddy
I ran into the same problem. After lots of digging, I found the solution. I'm using django 1.11.
我遇到了同样的问题。经过大量挖掘,我找到了解决方案。我正在使用 Django 1.11。
If you want to start-over,
如果你想重新开始,
1)delete all the files in your migrations folder except __init__.py
2)drop database
3)create database
4)python makemigrations
5)python migrate
if you have reset_db
, instead of 2nd and 3rd steps you can use reset_db
.
如果有reset_db
,您可以使用reset_db
.
python manage.py reset_db
回答by Ljubitel
In my case the __init__.py
file was missing from the APP/migrations/ folder. If you don't have one, all it needs is an empty __init__.py
file.
在我的情况下,该__init__.py
文件从 APP/migrations/ 文件夹中丢失。如果你没有,它所需要的只是一个空__init__.py
文件。
回答by Shamsul Arefin Sajib
- Delete database
- delete
migration
folder - run
migrate
command - run
makemigrations
command - run
migrate
command
- 删除数据库
- 删除
migration
文件夹 - 运行
migrate
命令 - 运行
makemigrations
命令 - 运行
migrate
命令
It will create all tables perfectly
它将完美地创建所有表
回答by Zahid Khan
Problem:: When you apply migrationsin django for the first time, django creates table of that model in database and marks somewhere in its own file(class):
问题::当您第一次在 django 中应用迁移时,django 在数据库中创建该模型的表并在其自己的文件(类)中标记某处:
`initial = True`
When you then tries to alter the schema of that table it firstly checks if
initial = True
if an?initial?class attribute isn't found, a migration will be considered “initial”
In case the
initial = True
we need to use?python manage.py migrate?--fake-initial
当您尝试更改该表的架构时,它首先检查是否
initial = True
如果未找到?initial?class 属性,则迁移将被视为“初始”
万一
initial = True
我们需要使用?python manage.py migrate?--fake-initial
For an initial migration Django checks that all of those tables already exist in the database and fake-applies the migration if so. Similarly, for an initial migration that adds one or more fields Django checks that all of the respective columns already exist in the database and fake-applies the migration if so.
对于初始迁移,Django 会检查所有这些表是否已存在于数据库中,如果存在则伪造应用迁移。类似地,对于添加一个或多个字段的初始迁移,Django 会检查所有相应的列是否已存在于数据库中,如果存在则伪造应用迁移。
Fake initial migration uses both CreateModel() and AddField() methods.
假初始迁移同时使用 CreateModel() 和 AddField() 方法。
Solution:
解决方案:
>> python manage.py makemigrations <AppName>
>> python manage.py migrate --fake-initial
回答by JavierFuentes
I am using MySQL and get into this issue after deleting 0001_initial.py
migration file and all the custom tables evolved from DB to try to regenerate all they...
我正在使用 MySQL 并在删除0001_initial.py
迁移文件和所有从 DB 演变而来的自定义表以尝试重新生成它们后遇到此问题...
Solvedthis issue simply deleting these rows in django_migrations
table...
解决了这个问题,只需删除django_migrations
表中的这些行...
After that, $ python manage.py migrate
command regenerates all my custom tables again.
之后,$ python manage.py migrate
命令再次重新生成我的所有自定义表。
回答by Kipngetich Yegon
To avoid deleting critical databases, you may also consider silencing properties
below the Class Meta:
for example this model:
为避免删除关键数据库,您还可以考虑properties
在以下Class Meta:
模型下进行静音:
class Blog(models.Model):
category = models.CharField(max_length=100)
title = models.CharField(max_length=100)
date_added = models.DateTimeField()
merits = models.CharField(max_length=300, blank=True)
demerits = models.CharField(max_length=300, blank=True)
class Meta:
managed = True
db_table = 'weblog'
verbose_name_plural = "Blog"
@property
def content_min(self):
return truncatechars(self.content, 50)
You can then run makemigrations
and migrate
and your table will be created.
然后您可以运行makemigrations
并migrate
创建您的表。