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.

1149 questions
559
votes
11 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
  • 7,790
  • 4
  • 28
  • 43
175
votes
32 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
  • 1,841
  • 2
  • 13
  • 9
170
votes
13 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
  • 8,735
  • 8
  • 41
  • 59
146
votes
7 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
  • 26,461
  • 53
  • 224
  • 476
145
votes
30 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,405
  • 2
  • 20
  • 24
125
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ä
  • 6,100
  • 5
  • 31
  • 38
113
votes
7 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,691
  • 5
  • 28
  • 37
92
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,080
  • 4
  • 22
  • 38
74
votes
4 answers

Django 1.8: Create initial migrations for existing schema

I started a django 1.8 project, which uses the migrations system. Somehow along the way things got messy, so I erased the migrations folders and table from the DB, and now I'm trying to reconstruct them, with no success. I have three apps (3…
user1102018
  • 4,041
  • 6
  • 21
  • 30
74
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
  • 25,661
  • 50
  • 173
  • 276
70
votes
11 answers

MySQL vs PostgreSQL? Which should I choose for my Django project?

My Django project is going to be backed by a large database with several hundred thousand entries, and will need to support searching (I'll probably end up using djangosearch or a similar project.) Which database backend is best suited to my project…
rmh
  • 4,466
  • 7
  • 30
  • 31
68
votes
3 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
  • 8,138
  • 10
  • 46
  • 102
67
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
  • 2,637
  • 3
  • 26
  • 29
66
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,340
  • 4
  • 14
  • 29
63
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
  • 7,629
  • 6
  • 25
  • 58
1
2 3
76 77