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
35
votes
16 answers

How to recreate a deleted table with Django Migrations?

There are two models Groups and Students and only one table for Groups of them, the Students table was deleted. How to make Django recreate the deleted table? If I do makemigrations it prints "No changes detected". On admin page when I click on the…
35
votes
4 answers

Revert Django 1.7 RemoveField migration

If I have a non-nullable model field, remove it, and create a migration, that migration becomes non-reversible: Consider the following model: class Foo(models.Model): bar = models.TextField() test = models.TextField() # This field is to go…
Ilya Semenov
  • 7,874
  • 3
  • 30
  • 30
34
votes
3 answers

--fake-initial vs --fake in Django migration?

What is the difference between --fake-initial and --fake in Django migrations? What are the dangers of using fake migrations? Anybody knows? Thank you very much to all. I am using django 1.10
yooth
  • 361
  • 1
  • 3
  • 4
33
votes
4 answers

Django: Best way to merge migrations conflicts

I'm currently working on a dev branch and I will need to merge it to master one day. I have up to 20 migrations files on my dev branch and about the same number on master at the moment. I needed to make migrations on both branches which will result…
scharette
  • 9,437
  • 8
  • 33
  • 67
33
votes
1 answer

Migrating ManyToManyField to null true, blank true, isn't recognized

I have made a model change from standard = models.ManyToManyField(Standard) to standard = models.ManyToManyField(Standard, blank=True, null=True) South schemamigration for this app doesn't recognize the change? Similar to this question, which is…
31
votes
4 answers

How to define default data for Django Models?

I want my application to have default data such as user types. What's the most efficient way to manage default data after migrations? It needs to handle situations such as, after I add a new table, it adds the default data for it.
Dhanushka Amarakoon
  • 3,502
  • 5
  • 31
  • 43
29
votes
2 answers

Django migrations with multiple databases

I have a hard time with creating data migrations. I use two databases for my apps. I configured databases in settings.py and also created a router like in Django docs. # settings.py DB_HOST = 'localhost' DATABASES = { 'default': { 'ENGINE':…
Leon Kladnitsky
  • 393
  • 1
  • 3
  • 7
29
votes
3 answers

How to add through option to existing ManyToManyField with migrations and data in django

I can't find reference to particular issue in docs or online. I have an existing many to many relation. class Books(models.Model): name = models.CharField(max_length=100) class Authors(models.Model): name =…
chhantyal
  • 11,874
  • 7
  • 51
  • 77
27
votes
3 answers

Django migrations using RunPython to commit changes

I want to alter a foreign key in one of my models that can currently have NULL values to not be nullable. I removed the null=True from my field and ran makemigrations Because I'm an altering a table that already has rows which contain NULL values in…
Gabriel Amram
  • 2,700
  • 2
  • 19
  • 29
26
votes
2 answers

Trying to make a PostgreSQL field with a list of foreign keys in Django

Here is what I'm trying to do: Make a model in Django that is a PostgreSQL array (database specific type), which contains foreign keys to another model. class Books(models.Model): authors = ArrayField( models.ForeignKey('my_app.Authors',…
Danil
  • 3,348
  • 2
  • 20
  • 16
25
votes
3 answers

IntegrityError: null value in column "id" for all models/fields with ForeignKey after postgres restore from dump

I'm running into issues trying to use a heroku postgres datastore from a restore of a local postgres database I have. Using the restored postgres database Django runs as normal. It retrieves all objects and uses their fields, primay key's etc…
taylor
  • 509
  • 1
  • 9
  • 22
25
votes
3 answers

Django 1.7 - makemigrations creating migration for unmanaged model

I am creating some dynamic Django models in my application and everything seems to be working as expected except for the migration system. If I create a dynamic Django model and set managed = False, Django's makemigrations command still generates a…
chadgh
  • 9,063
  • 8
  • 38
  • 54
24
votes
5 answers

Django Heroku Error "Your models have changes that are not yet reflected in a migration"

I recently added a model to my app (UserProfile) and when I pushed the changes to Heroku, I think I accidentally ran heroku run python manage.py makemigrations. Now when I try to run heroku run python manage.py migrate I get the error…
Ben
  • 20,038
  • 30
  • 112
  • 189
23
votes
5 answers

Django 1.7 Migrations

I am using django 1.7 and I just added a custom user model. When I run either python3 manage.py makemigrations or python3 manage.py migrate I get the error: TypeError: __init__() got an unexpected keyword argument 'preserve_default'. This issue came…
Denny
  • 1,739
  • 3
  • 20
  • 39
22
votes
1 answer

How to create GIN index in Django migration

In Django, since version 1.11 we have a class for PostgreSQL GinIndex (https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/). I'd like to create a migration that constructs such index on a VectorSearchField I added to one of my…
Maciek
  • 3,174
  • 1
  • 22
  • 26