Questions tagged [django-migrations]

Django migrations are a way to apply changes to a database previously created, introduced in Django 1.7. This tool is used when a model is modified (adding a field, deleting a model, etc.) and you need to apply these changes to your database.

Migrations are introduced in Django 1.7 to make easier modifications in models:

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into.

There are several commands which you will use to interact with migrations and Django’s handling of database schema:

  • migrate, which is responsible for (un)applying migrations
  • makemigrations, to create new migrations files based on model modifications
  • sqlmigrate, which displays the SQL statements for a migration.
  • showmigrations, which lists a project’s migrations and their status.

This tag is not about migrating to Django from another framework. This is supposed to replace , which is a third-party mainly used until Django 1.6 to migrate models modifications.

1460 questions
750
votes
14 answers

How to revert the last migration?

I've made a migration that added a new table and want to revert it and delete the migration, without creating a new migration. How do I do it? Is there a command to revert last migration and then I can simply delete the migration file?
Ronen Ness
  • 9,923
  • 4
  • 33
  • 50
270
votes
38 answers

Django - makemigrations - No changes detected

I was trying to create migrations within an existing app using the makemigrations command but it outputs "No changes detected". Usually I create new apps using the startapp command but did not use it for this app when I created it. After debugging,…
Dilraj
  • 2,791
  • 2
  • 15
  • 9
208
votes
14 answers

Django migration strategy for renaming a model and relationship fields

I'm planning to rename several models in an existing Django project where there are many other models that have foreign key relationships to the models I would like to rename. I'm fairly certain this will require multiple migrations, but I'm not…
Fiver
  • 9,909
  • 9
  • 43
  • 63
188
votes
10 answers

Django-DB-Migrations: cannot ALTER TABLE because it has pending trigger events

I want to remove null=True from a TextField: - footer=models.TextField(null=True, blank=True) + footer=models.TextField(blank=True, default='') I created a schema migration: manage.py schemamigration fooapp --auto Since some footer columns…
guettli
  • 25,042
  • 81
  • 346
  • 663
150
votes
36 answers

Django 1.7 - makemigrations not detecting changes

As the title says, I can't seem to get migrations working. The app was originally under 1.6, so I understand that migrations won't be there initially, and indeed if I run python manage.py migrate I get: Operations to perform: Synchronize…
TyrantWave
  • 4,583
  • 2
  • 22
  • 25
142
votes
2 answers

How to migrate back from initial migration in Django 1.7?

I created a new app with some models and now I noticed that some of the models are poorly thought out. As I haven't committed the code the sensible thing would be to migrate the database to last good state and redo the migration with better models.…
Seppo Erviälä
  • 7,160
  • 7
  • 35
  • 40
118
votes
8 answers

Disable migrations when running unit tests in Django 1.7

Django 1.7 introduced database migrations. When running the unit tests in Django 1.7, it forces a migrate, that takes a long time. So I would like to skip the django migrations, and create the database in the final state. I know that ignoring the…
David Arcos
  • 5,957
  • 5
  • 30
  • 39
114
votes
19 answers

Django: OperationalError No Such Table

I'm building a fairly simple application, research, in my Django project that uses Django-CMS. (It's my first ground-up attempt at a project/application.) Its main purpose is to store various intellectual assets (i.e article, book, etc. written by…
Brian
  • 1,729
  • 2
  • 14
  • 17
111
votes
1 answer

Django migrate --fake and --fake-initial explained

I've been a user of Django for about 2 years now and there is a feature I have always been afraid of using : faking migrations. I've looked pretty much everywhere and the most information I can get is from the documentation where it states…
scharette
  • 9,437
  • 8
  • 33
  • 67
102
votes
4 answers

How to squash recent Django migrations?

In Django's migrations code, there's a squashmigrations command which: "Squashes the migrations for app_label up to and including migration_name down into fewer migrations, if possible." So, if you want to squash, say, the first 5 migrations, this…
Doug Harris
  • 3,169
  • 5
  • 29
  • 31
94
votes
3 answers

django 1.7 migrate gets error "table already exists"

I am trying to apply a migration but am getting the error: django.db.utils.OperationalError: (1050, "Table 'customers_customer' already exists") I get this by issuing the following command: python manage.py migrate My customer table already…
Atma
  • 29,141
  • 56
  • 198
  • 299
93
votes
11 answers

How to simplify migrations in Django 1.7?

There are already similar questions for South, but I have started my project with Django 1.7 and am not using South. During development a lot of migrations have been created, however the software is not yet delievered and there exists no database…
Kit Fisto
  • 4,385
  • 5
  • 26
  • 43
92
votes
13 answers

How to reset migrations in Django 1.7

(I know there is a title the same as this, but the question is different). I have managed to get my development machine migrations and production migrations out of sync. I have a Django app which was using South. I had my own workflow that worked…
wobbily_col
  • 11,390
  • 12
  • 62
  • 86
90
votes
4 answers

Django Migrations Add Field with Default as Function of Model

I added a new, non-nullable field to my Django model and am trying to use migrations to deploy that change. How would I set default value to use for existing models to be some function of those models rather than a constant? As an example let's say…
Alex Rothberg
  • 10,243
  • 13
  • 60
  • 120
89
votes
1 answer

Django 1.8 Run a specific migration

In django 1.8 is there a way to run a specific migration and that migration only. Not for one app only but a specific file in that apps migrations directory. EDIT TO ORIGINAL: Traceback (most recent call last): File "manage.py", line 10, in…
bgrantdev
  • 1,622
  • 4
  • 17
  • 29
1
2 3
97 98