2

I have existing database and no migrations files (which was lost for some reasons). I have some new models, which are not correspond to existing database, some tables are altered in comparison with existing database. I'd like to synchronize db and models, means alter some tables in existing database.

I have try this:

Django 1.8: Create initial migrations for existing schema

but when run manage migrate --fake-initial (or --fake) my first migration (created from new models) are marked as FAKED, so, if I got it well, fully ignored, so no tables are altered.

Are there any way to automatically synchronize db and models (w/o exporting/importing data from/to db)? Somehow make initial migration as snapshot of existing database and then make migrations, relative to it?

john.don83
  • 103
  • 1
  • 10
  • 3
    No, there isn't an easy way to do this. The `makemigrations` command creates migrations based on the models, not on the current database. You might be able to use `inspectdb` to create a `models.py` that matches your existing database, and use that to create your initial migration. – Alasdair Aug 21 '18 at 15:32
  • Are there any external tools, no? – john.don83 Aug 21 '18 at 15:38
  • Not that I'm aware of – Alasdair Aug 21 '18 at 15:42
  • Seems really crazy, but I'll be able so synchronize only through full recreation of db. inspectdb creates models, which are not suitable for makemigrations at all at my case. – john.don83 Aug 21 '18 at 16:42
  • 1
    Yes, `inspectdb` creates models based on your current database. You can use these to create initial migrations, then switch back to your current models and create migrations to upgrade your database. As I said in my initial comment, it isn’t straight forward. Keep your migrations backed up and under version control in future, so that you don’t hit issues like this. – Alasdair Aug 21 '18 at 19:10

0 Answers0