Opened 5 years ago
Closed 5 years ago
#31669 closed Bug (invalid)
migrate --fake-initial doesn't work when only a subset of tables exists in database.
Reported by: | Sara Correia | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 3.0 |
Severity: | Normal | Keywords: | |
Cc: | Michael XU | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
I have a migration file with the creation of two models: A and B. The database already has the table corresponding to the model A.
So, when I run the command python manage.py migrate --fake-initial I get an exception "jango.db.utils.ProgrammingError: relation "A" already exists.
The migration should ignore the existing tables, but crate the new ones. Otherwise, it's impossible to have a initial migration file with existing tables and new ones.
Sara
Change History (2)
comment:1 by , 5 years ago
Cc: | added |
---|
comment:2 by , 5 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Summary: | migrate --fake-initial doesn't work when only a subset of tables exists in database → migrate --fake-initial doesn't work when only a subset of tables exists in database. |
This is an intended and documented behavior.
Hi - I can confirm this behavior, but I think it is the intended behavior.
The migration should ignore the existing tables, but crate the new ones.
I feel like this quote above is not the intended behavior. In the doc it mentions two conditions:
To me, it would sound like your case has violated at least one of the conditions - perhaps model B has been added after the tables were made? There are other possibilities, of course.
On another hand, looking at the code, when we do
python manage.py migrate --fake-initial
, it will check if all the tables and columns mentioned in the migration file already exists in the database, and if not, it will, as it says in the comment, "Alright, do it normally", which would then throw the error you have seen:django.db.utils.OperationalError: table "<...>" already exists
.In this sense,
fake-initial
seems to only befake
with an additional check: That all the tables and columns are already in your database, and if this is not satisfied, it will try to apply the migrations as usual.