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
14
votes
2 answers

django: 'python manage.py migrate' taking hours (and other weird behavior)

I made some changes to one of my tables in models.py and tried migrating it with 'python manage.py migrate,' and it's taking hours. I only changed the names of three field (column) names, and it's been running for over 2 hours now. It ran smoothly…
user20408
  • 679
  • 2
  • 7
  • 23
13
votes
1 answer

Django 2.1 how do I create a user in a migration

I'm trying to add a new user and then associate it to a subset of existing records. For example, imagine I have this app model: class Foo(Model): user = model.ForeignKey(User, default=None, on_delete=models.CASCADE) In my migration I have: from…
Ben
  • 10,931
  • 9
  • 38
  • 47
13
votes
2 answers

django.db.utils.OperationalError: cannot ALTER TABLE because it has pending trigger events

I am trying to change accepted_answer ForeignKey to a BooleanField and while migrating getting the error django.db.utils.OperationalError: cannot ALTER TABLE "forum_thread" because it has pending trigger events. This is the models.py of…
Ranvijay Sachan
  • 2,407
  • 3
  • 30
  • 49
13
votes
3 answers

Django proxy model to different database

Situation We have a few different applications which use tickets from a ticket support system for different kinds of functionality. First of all we have an application which has a few models that represent the models of our ticket support system…
Bono
  • 4,757
  • 6
  • 48
  • 77
13
votes
3 answers

How to add a permission to a user/group during a django migration?

I would like to execute the following migration: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib.auth.models import Permission from django.db import migrations from django.conf import settings from…
13
votes
2 answers

Why use apps.get_model() when creating a data migration?

As per the django docs when creating django migrations we should use apps.get_model() rather than importing the models and using them. Why does a data migration have to use the historical version of a model rather than the latest one?(The historical…
user3282666
  • 640
  • 1
  • 7
  • 21
13
votes
1 answer

Django datetime default value in migrations

I've read some questions about this issue in so, also this question is not giving the correct answer for my case: I'm adding a created_time field in my already existing models, so no date in the mysql table belonging to the model. class…
Evhz
  • 8,852
  • 9
  • 51
  • 69
13
votes
1 answer

Django makemigrations app order

I'm using Django 1.8.4. As my project is still under construction, I frequently remove all migration scripts, and rerun makemigrations to generate the initial migration scripts. I found makemigrations would generate two migration scripts for one of…
Han He
  • 3,391
  • 3
  • 24
  • 25
13
votes
1 answer

Rolling back to a previous migration in django

I used my django migrations to migrate my database. One of the migrations was data migrations. But I made a mistake: I didn't save the model). Thus, no change was applied to data. I corrected the .py file that executed the python code for the data…
Apostolos
  • 7,763
  • 17
  • 80
  • 150
12
votes
1 answer

Cannot cast type integer to uuid

I tried to run python3 manage.py migrate, but i got this error: File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django /db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql,…
12
votes
1 answer

Django backup strategy with dumpdata and migrations

As in this question, I set up a dumpdata-based backup system for my database. The setup is akin to running a cron script that calls dumpdata and moves the backup to a remote server, with the aim of simply using loaddata to recover the database.…
Felipe
  • 3,003
  • 2
  • 26
  • 44
12
votes
1 answer

Error when reverting an auto-generated migration for renaming a table in Django

I'm having issues with reverting a Django (1.8.7) migration that contains the renaming of a table. Even though it seems to be able to rename it in Postgres, it then tries to add a constraint using the old table name. Here's the traceback: …
12
votes
5 answers

Django 1.7 makemigrations freezing/hanging

I'm finally upgrading from Django 1.6 to 1.7, and removing South in the process. I followed the official Django instructions and removed all my old numbered migrations. Now I'm trying to run python manage.py makemigrations to get the new…
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
11
votes
2 answers

Migrating models of dependencies when changing DEFAULT_AUTO_FIELD

I'm using Django 3.2. I've changed added this line to settings.py: DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' I then ran these commands: $ python manage.py makemigrations $ python manage.py migrate The makemigrations command creates new…
Flimm
  • 136,138
  • 45
  • 251
  • 267
11
votes
2 answers

Best practice when add a new unique field to an existing django model

I have an existing model that looks somewhat like the following... class Resource(models.Model): id = models.AutoField(primary_key=True) We have been using this for some time, and now have ~1M instances of these Resource objects (and…
wakey
  • 2,283
  • 4
  • 31
  • 58