0

I'm sorry if the name of the question is misleading but here is the deal.

I have dump of the database that was used with old version of django app (django < 1.7).

I have a new version of code which is using django 1.7. And now I need to upgrade some server with new code while saving all data.

How I thought it would work:

Restore database, run new migration (1.7 migrations) with ./manage.py migrate.

Done!

But when I'm running migrations I have "Relation already exists error".

I know that this is happening probably because database is out of sync with migration history or something... But I don't know what to do.

EDIT1 I sense that the only way is manually create migration script or something, because there is now way to sync database with new migrations now.

So suppose a have a table in database named TABLE and two columns C1 and C2.

Now when I was migrating from 1.6 to 1.7 I've added column C3. So the initial migration looks something like this "create table TABLE with COLUMNS C1, C2, C3".

And when I will try to migrate with old database it wouldn't be ably to do this.

user1685095
  • 5,787
  • 9
  • 51
  • 100

1 Answers1

0
  1. Delete new columns.
  2. Create initial migrations.
  3. Fake initial migrations for all apps:

    python manage.py migrate --fake yourappnamehere 0001
    
  4. Add columns, create new migrations.
  5. Execute new migrations.
zymud
  • 2,221
  • 16
  • 24
  • Um... I don't know what columns are new. So this would require manual check. Not really an option. 2. I already have migrations. Why do I need to create another one? Wouldn't it break another migrations? – user1685095 Jan 28 '15 at 12:46
  • 1. You can create django models from your existing database: `python manage.py inspectdb > models.py` and check what columns are new. If you db is really huge you can write simple parser which will compare your models and generated from your base. 2. As I understand your current migrations do not match database, so they have to be deleted. – zymud Jan 28 '15 at 13:27
  • No, they don't have to be deleted. The database should be changed to the state where it would be consistent with new migrations, not otherwise =) – user1685095 Jan 28 '15 at 13:29
  • And yes, I get it. I probably should delete the question, because I thought about this done automatially using the migration and django instruments. Of course I can do this manually, delete here and there and sync database and migrations, but that is not productive, so it's not interesting to me. While I still think that migrations in django should be able to do that. – user1685095 Jan 28 '15 at 13:32
  • I mean, They can sync empty database with migrations, right? So to sync not empty database with migrations is just one step further. First - from migrations find what current schema should look like, then inspect actual schema, and then make actual schema current schema adding new tables, columns or deleting them. Seems pretty easy. – user1685095 Jan 28 '15 at 13:34
  • Yes your idea is right, but, unfortunately, I do not know way to do this absolutely automatically – zymud Jan 28 '15 at 13:37