简体   繁体   中英

Django missing migrations file - how to sync db with master file?

one of my fellow developers checked out from master and created new models for our website. He ran makemigrations, then ran migrate which obviously created the tables we wanted.

However, he never committed his changes to github and he altered the production database. So when I went in to add a table today, when I ran makemigrations it the terminal listed several tables that I knew already existed...I was like "YOLO!" and ran the migrate command anyways and it puked.

So, since the migrations file isn't in my migrate folder, django thinks it needs to create those tables...then it goes to create them and pukes because they're already there.

The other developer is out of town visiting family and can't commit the file.

How do I get this set straight? I think I need to run ./manage.py migrate my_app --fake

But I don't completely understand what that does so I don't want to take the YOLO route and really mess things up...

OK, I promise everybody out there that I have been working on this problem for 9.5 hours today. Turns out this was the answer:

Django migrations : relation already exists

However, there were some spelling/syntax errors that made it difficult to understand that this other person had the same problem as me.

to reiterate the solution:

  1. type: ./manage.py makemigrations your_app
  2. Navigate to the my_app/migrations folder and open the migrations file that was just created (usually looks like '0005_modelsandstuff_blablabla.py"
  3. Delete the models that DON'T ALREADY EXIST. Save the file and close
  4. type: ./manage.py migrate your_app --fake
  5. this will then sync what is in the database now with your models schema without altering any of the actual database
  6. type: ./manage.py makemigrations your_app
  7. type: ./manage.py migrate your_app

And that's it! Everything is all synced up again. Just as a quick jab I would like to say JavaScript sucks. Thanks.

Following these steps should solve your problem.

  1. Backup your database
  2. Stash your changes (so that only the missing schema changes are picked up)
  3. Create the migrations (this creates the already applied schema changes)
  4. Run migrate with --fake (this will fake apply the already done schema changes)
  5. Apply your changes
  6. Create the migrations
  7. Run migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM