--fake-initial vs --fake in Django migration?

2019-02-08 01:28发布

What is the difference between --fake-initial and --fake in Django migrations? What are the dangers of using fake migrations? Anybody knows? Thank you very much to all.

I am using django 1.10

2条回答
\"骚年 ilove
2楼-- · 2019-02-08 01:35

Well the documentation is very clear about this

--fake-initial

Allows Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names

You were asking about the risks, well here it is

only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.

--fake

Tells Django to mark the migrations as having been applied or unapplied, but without actually running the SQL to change your database schema.

This is intended for advanced users to manipulate the current migration state directly if they’re manually applying changes;

Once again risks are clearly highlighted

be warned that using --fake runs the risk of putting the migration state table into a state where manual recovery will be needed to make migrations run correctly.

This answer is valid not just for django versions 1.8+ but for other versions as well.

edit Nov, 2018: I sometimes I see answers here and elsewhere that suggest that you should drop your databae. That's almost never the right thing to do. If you drop your database you lose all your data.

查看更多
Explosion°爆炸
3楼-- · 2019-02-08 01:52

@e4c5 already gave an answer about this question, but I would like to add one more thing concerning when to use --fake and --fake-initial.

Suppose you have a database from production and you want to use it for development and apply migrations without destroying the data. In that case --fake-initial comes in handy.

The --fake-initial will force Django to look at your migration files and basically skip the creation of tables that are already in your database. Do note, though, that any migrations that don’t create tables (but rather modify existing tables) will be run.

Conversely, If you have an existing project with migration files and you want to reset the history of existing migrations, then --fake is usually used.

查看更多
登录 后发表回答