3

I am building an app in Django and it uses a live/in-use database.

Basically since the apps development the SQL Database has undergone some structure changes and it is causing issues with Django, Django will try to apply migrations to the database that already exist. For example:

In the Django app I marked the email column as unique which was fine based on the development database. However the main database now always has a table change that marks the email column as unique. Django is fighting this unique key with the one that already exists.

So is it possible to clear all Django migrations and have it make migrations again compared to the more up to date SQL database structure?

Chaos
  • 471
  • 4
  • 20
  • You have to choose if you want Django to manage your database. If you want to make your own changes to the db and have Django adopt the changes afterwards, instead of Django changing the database itself, consider using [`managed = False`](https://docs.djangoproject.com/en/1.10/ref/models/options/#managed) in your models. – knbk Nov 26 '16 at 02:54

1 Answers1

1

If your models are very out of sync with your database, the easiest option is probably to rebuild your models from scratch using inspectdb.

If your models are pretty close to the database already, the first step is to make sure that your models match the database exactly. You can use sqldiff from django-extensions for this.

Once your Django models match your database, follow this answer to re-create migrations based on the existing database schema.

Community
  • 1
  • 1
YPCrumble
  • 26,610
  • 23
  • 107
  • 172
  • @bmazoka The key is that the tables you're using in your database and your app's models need to be identical. One option is to take a copy of the database on your local machine and delete any of the tables you are not using. Then you can create your migrations using the steps above. – YPCrumble Nov 25 '16 at 20:32
  • 1
    Deleting the migrations (e.g., with `rm -rf **/migrations`) is fine unless there are any custom migrations in place. Best practice is to make sure the migrations are part of your git repo so that you can get them back just in case. – YPCrumble Nov 26 '16 at 15:35