Mark O
Mark O

Reputation: 1

Migrating to existing database without matching migration

I am working on a website in Django 1.9.8, and am trying to deploy an updated version of it to a server to demo it.

As I have been learning Django as I develop this, I have made the mistake a few times of deleting my migrations and creating new ones when I run into database issues. This works fine on my dev when I am able to just delete an existing database, but is causing issues trying to get this deployed on the demo server without loosing the existing database data.

I don't have the migration for the existing database schema on the demo server, and when I run manage.py makemigration <myappname>, it creates a initial migration for the app. When I attempt to run manage.py migrate <myappname>, it tells me that the tables already exist (because the migration is trying to add all of those tables, rather than do the modifications needed to be done on the database).

Is there a way to create an initial migration based off of the current database, that I could then run makemigrations to get a migration that will modify the current database to be up to date with the current model?

Appologies if this is already answered somewhere, I have looked and had no lucking finding an answer.

Thanks!

Upvotes: 0

Views: 1210

Answers (1)

David K.
David K.

Reputation: 116

Django thinks the migrations have already been run because there are entries for the migrations in the django_migrations table. That is where django registers the migrations that have already been completed. You need to go into postres and delete the rows for those migrations you do not want. This is assuming you db schema matches your models.py. If there is a difference you need to manually edit the migration files to remove any discrepancies, or modify you models.py temporariliy to match up.

psql [db name]
delete from django_migrations;

Now delete all migrations folders in your apps and run: ./manage.py migrate --fake-initial

If your models.py or migrations were edited to match the current db schema and you now want to run a migration on the differences, run ./manage.py makemigrations and then migrate

Also look here: Django 1.8: Create initial migrations for existing schema

Upvotes: 1

Related Questions