Django重置migration
清空数据库
不需要原有的数据库数据
- 删除数据库所有的表
- 删除项目的migrations模块中的所有 文件,除了
__init__.py
文件 - 运行命令
python manage.py makemigrations
python manage.py migrate
重置migrations
不想要删除现有的数据库,只是想重新建立 migrations 文件,强迫症的福音
重置所有步骤
manage.py@StarMeow > makemigrations
manage.py@StarMeow > showmigrations casual
manage.py@StarMeow > migrate --fake casual zero # 步骤
manage.py@StarMeow > showmigrations casual
# 手动删除文件
manage.py@StarMeow > makemigrations casual # 步骤
manage.py@StarMeow > migrate --fake-initial casual # 步骤
检查同步
保证目前的migrations
文件和数据库是同步的,通过执行
manage.py@StarMeow > makemigrations
# 如果看到 这样的提示: No changes detected,则可以继续接下来的步骤
查看所有已生效migrations文件
可以看到当前项目,所有的app及对应的已经生效的migrations文件如
manage.py@StarMeow > showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
assets
[X] 0001_initial
[X] 0002_auto_20181205_0926
[X] 0003_floorareaseat
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
blog
[X] 0001_initial
[X] 0002_blogcomment
[X] 0003_auto_20180821_1231
captcha
[X] 0001_initial
casual
[X] 0001_initial
[X] 0002_auto_20180922_1548
[X] 0003_auto_20180922_1549
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
coolqbot
(no migrations)
log
[X] 0001_initial
[X] 0002_auto_20181115_2114
[X] 0003_blogdailytimes
problem
[X] 0001_initial
[X] 0002_auto_20181225_1924
[X] 0003_auto_20181228_1529
pxectrl
[X] 0001_initial
[X] 0002_auto_20181230_1523
[X] 0003_auto_20190102_1547
reversion
[X] 0001_squashed_0004_auto_20160611_1202
schedule
[X] 0001_initial
[X] 0002_auto_20181126_1338
[X] 0003_auto_20181126_1900
sessions
[X] 0001_initial
simpleauth
[X] 0001_initial
[X] 0002_auto_20181206_1126
[X] 0003_simpleconfigure
supervise
[X] 0001_initial
[X] 0002_auto_20181217_1206
usercenter
[X] 0001_initial
[X] 0002_auto_20180820_1418
[X] 0003_userpermission
webchat
(no migrations)
xadmin
[X] 0001_initial
[X] 0002_log
[X] 0003_auto_20160715_0100
查看某个应用已生效migrations文件
只查看某个应用
manage.py@StarMeow > showmigrations casual
casual
[X] 0001_initial
[X] 0002_auto_20180922_1548
[X] 0003_auto_20180922_1549
[X] 0004_auto_20180922_1559
[X] 0005_auto_20180923_0950
[X] 0006_auto_20180924_1633
[X] 0007_buyerinfo_group_name
[X] 0008_auto_20181202_1701
[X] 0009_auto_20181202_1719
[X] 0010_auto_20181217_1206
[X] 0011_auto_20181217_1311
[X] 0012_auto_20181217_1428
[X] 0013_auto_20181217_1429
[X] 0014_auto_20181217_1430
[X] 0015_auto_20181217_1728
[X] 0016_works_url
[X] 0017_auto_20181217_1741
[X] 0018_auto_20181218_1307
[X] 0019_auto_20181218_1358
[X] 0020_auto_20181219_1315
[X] 0021_auto_20181219_1706
[X] 0022_auto_20181221_2322
[X] 0023_auto_20190108_1234
开始重置某个应用
manage.py@StarMeow > migrate --fake casual zero
Operations to perform:
Unapply all migrations: casual
Running migrations:
Rendering model states... DONE
Unapplying casual.0023_auto_20190108_1234... FAKED
Unapplying casual.0022_auto_20181221_2322... FAKED
Unapplying casual.0021_auto_20181219_1706... FAKED
Unapplying casual.0020_auto_20181219_1315... FAKED
Unapplying casual.0019_auto_20181218_1358... FAKED
Unapplying casual.0018_auto_20181218_1307... FAKED
Unapplying casual.0017_auto_20181217_1741... FAKED
Unapplying casual.0016_works_url... FAKED
Unapplying casual.0015_auto_20181217_1728... FAKED
Unapplying casual.0014_auto_20181217_1430... FAKED
Unapplying casual.0013_auto_20181217_1429... FAKED
Unapplying casual.0012_auto_20181217_1428... FAKED
Unapplying casual.0011_auto_20181217_1311... FAKED
Unapplying casual.0010_auto_20181217_1206... FAKED
Unapplying casual.0009_auto_20181202_1719... FAKED
Unapplying casual.0008_auto_20181202_1701... FAKED
Unapplying casual.0007_buyerinfo_group_name... FAKED
Unapplying casual.0006_auto_20180924_1633... FAKED
Unapplying casual.0005_auto_20180923_0950... FAKED
Unapplying casual.0004_auto_20180922_1559... FAKED
Unapplying casual.0003_auto_20180922_1549... FAKED
Unapplying casual.0002_auto_20180922_1548... FAKED
Unapplying casual.0001_initial... FAKED
这里的 casual
就是你要重置的app
查看重置后的状态
之后再执行 python manage.py showmigrations
,你会发现 文件前的 [x] 变成了[ ]
manage.py@StarMeow > showmigrations casual
casual
[ ] 0001_initial
[ ] 0002_auto_20180922_1548
[ ] 0003_auto_20180922_1549
[ ] 0004_auto_20180922_1559
[ ] 0005_auto_20180923_0950
[ ] 0006_auto_20180924_1633
[ ] 0007_buyerinfo_group_name
[ ] 0008_auto_20181202_1701
[ ] 0009_auto_20181202_1719
[ ] 0010_auto_20181217_1206
[ ] 0011_auto_20181217_1311
[ ] 0012_auto_20181217_1428
[ ] 0013_auto_20181217_1429
[ ] 0014_auto_20181217_1430
[ ] 0015_auto_20181217_1728
[ ] 0016_works_url
[ ] 0017_auto_20181217_1741
[ ] 0018_auto_20181218_1307
[ ] 0019_auto_20181218_1358
[ ] 0020_auto_20181219_1315
[ ] 0021_auto_20181219_1706
[ ] 0022_auto_20181221_2322
[ ] 0023_auto_20190108_1234
删除migrations文件夹下的文件
现在,你可以删除casual
这个 app下的migrations模块中 除 __init__.py
之外的所有文件。
重新生成migrations文件
之后,执行
manage.py@StarMeow > makemigrations casual
Migrations for 'casual':
apps\casual\migrations\0001_initial.py
- Create model BuyerInfo
- Create model Category
- Create model Discount
- Create model GroupBuyingInfo
- Create model GroupBuyingWorks
- Create model Phase
- Create model PostageLink
- Create model PostageTemplate
- Create model PurchaseOrder
- Create model PurchaseRecord
- Create model Works
- Add field works to purchaserecord
- Add field works to groupbuyingworks
- Add field phase to groupbuyinginfo
- Add field phase to discount
- Alter unique_together for works (1 constraint(s))
- Alter unique_together for discount (1 constraint(s))
程序会再次为这个app 生成 0001_initial.py
之类的文件
重要:重置migrations到数据库
最重要的一步来了, 执行
manage.py@StarMeow > migrate --fake-initial casual
Operations to perform:
Apply all migrations: casual
Running migrations:
Applying casual.0001_initial... FAKED
--fake-initial
会在数据库中的 migrations表中记录当前这个app 执行到 0001_initial.py
,但是它不会真的执行该文件中的 代码。
这样就做到了,既不对现有的数据库改动,而又可以重置 migraion 文件
参考:https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html
英文文档
Scenario 1:
The project is still in the development environment and you want to perform a full clean up. You don’t mind throwing the whole database away.
1. Remove the all migrations files within your project
Go through each of your projects apps migration folder and remove everything inside, except the __init__.py
file.
Or if you are using a unix-like OS you can run the following script (inside your project dir):
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
2. Drop the current database, or delete the db.sqlite3
if it is your case.
3. Create the initial migrations and generate the database schema:
python manage.py makemigrations
python manage.py migrate
And you are good to go.
Scenario 2:
You want to clear all the migration history but you want to keep the existing database.
1. Make sure your models fits the current database schema
The easiest way to do it is trying to create new migrations:
python manage.py makemigrations
If there are any pending migration, apply them first.
If you see the message:
No changes detected
You are good to go.
2. Clear the migration history for each app
Now you will need to clear the migration history app by app.
First run the showmigrations
command so we can keep track of what is going on:
$ python manage.py showmigrations
Result:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
core
[X] 0001_initial
[X] 0002_remove_mymodel_i
[X] 0003_mymodel_bio
sessions
[X] 0001_initial
Clear the migration history (please note that core is the name of my app):
$ python manage.py migrate --fake core zero
The result will be something like this:
Operations to perform:
Unapply all migrations: core
Running migrations:
Rendering model states... DONE
Unapplying core.0003_mymodel_bio... FAKED
Unapplying core.0002_remove_mymodel_i... FAKED
Unapplying core.0001_initial... FAKED
Now run the command showmigrations
again:
$ python manage.py showmigrations
Result:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
core
[ ] 0001_initial
[ ] 0002_remove_mymodel_i
[ ] 0003_mymodel_bio
sessions
[X] 0001_initial
You must do that for all the apps you want to reset the migration history.
3. Remove the actual migration files.
Go through each of your projects apps migration folder and remove everything inside, except for the __init__.py
file.
Or if you are using a unix-like OS you can run the following script (inside your project dir):
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
PS: The example above will remove all the migrations file inside your project.
Run the showmigrations
again:
$ python manage.py showmigrations
Result:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
core
(no migrations)
sessions
[X] 0001_initial
4. Create the initial migrations
$ python manage.py makemigrations
Result:
Migrations for 'core':
0001_initial.py:
- Create model MyModel
5. Fake the initial migration
In this case you won’t be able to apply the initial migration because the database table already exists. What we want to do is to fake this migration instead:
$ python manage.py migrate --fake-initial
Result:
Operations to perform:
Apply all migrations: admin, core, contenttypes, auth, sessions
Running migrations:
Rendering model states... DONE
Applying core.0001_initial... FAKED
Run showmigrations
again:
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
core
[X] 0001_initial
sessions
[X] 0001_initial
And we are all set up :-)