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

Why does Django create migration files for proxy models?

I just created a proxy model and was surprised that manage.py makemigrations creates a new migration file with a migrations.CreateModel operation. A proxy model does not create a new database table, it's just a different python interface to the same…
Maxime R.
  • 9,621
  • 7
  • 53
  • 59
22
votes
6 answers

django.db.utils.IntegrityError: column "venue_city" contains null values

I have read a lot of other posts here on stackoverflow and google but I could not find a solution. It all started when I changed the model from a CharField to a ForeignKey. The error I recieve is: Operations to perform: Synchronize unmigrated…
Tony
  • 2,382
  • 7
  • 32
  • 51
22
votes
4 answers

Django migrations and FileSystemStorage depending on settings

In my Django app I use a FileSystemStorage for generated files. I initialize it like this: import os from urlparse import urljoin from django.conf import settings from django.core.files.storage import FileSystemStorage gen_files_storage =…
geckon
  • 8,316
  • 4
  • 35
  • 59
22
votes
2 answers

Add non-null and unique field with already populated model

I have one model in my app running in a server with a few entries. I need to add a SlugField, unique and not-null for this model. The SlugField will be populated based on trading_name. I've changed my model in order to add this new field and…
MatheusJardimB
  • 3,599
  • 7
  • 46
  • 70
22
votes
3 answers

Adding indexes to model fields in Django with migrations

I am trying to add indexes on model fields using Field.db_index for an app that has migrations. Looking at Django's documentation all I need to do is to set db_index=True: class Person(models.Model): first_name = models.CharField() last_name…
Nima
  • 3,129
  • 1
  • 21
  • 20
21
votes
6 answers

MakeMigration Error on Django - ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models'

I got the following error after adding a new model field and running the makemigrations command: ImportError: cannot import name 'FieldDoesNotExist' from 'django.db.models' (/usr/local/lib/python3.7/site-packages/django/db/models/init.py) this is…
Okey Chima
  • 275
  • 1
  • 2
  • 9
21
votes
6 answers

Django Migration RunSQL Conditional on Database Type

I am trying to use a migrations.RunSQL Django migration to run some arbitrary SQL code. I would like to run this migration for only certain db backends (for example only for postgres). I would think to use something like this but I don't see the DB…
Alex Rothberg
  • 10,243
  • 13
  • 60
  • 120
21
votes
1 answer

Django migration fails with "__fake__.DoesNotExist: Permission matching query does not exist."

In a Django 1.8 project, I have a migration that worked fine, when it had the following code: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import migrations from django.conf import settings def…
das-g
  • 9,718
  • 4
  • 38
  • 80
21
votes
1 answer

How to unapply the very first migration in django 1.7

It looks like rolling back to any migration is possible using: ./manage.py migrate However, this requires an actual migration to be applied, and I need to rollback the very first migration! Specifically, I want to…
moomima
  • 1,200
  • 9
  • 12
20
votes
1 answer

Querying django migrations table

How can one query the django_migrations table from a view? For instance(What I have tried and of course not working) from django.db import migrations latest_migration = migrations.objects.all().order_by('-applied')[0] If possible, how should one…
Olfredos6
  • 808
  • 10
  • 19
20
votes
2 answers

Trying to migrate in Django 1.9 -- strange SQL error "django.db.utils.OperationalError: near ")": syntax error"

I don't have a clue what's causing this error. It appears to be a bug that there isn't a fix for. Could anyone tell give me a hint as to how I might get around this? It's frustrating me to no end. Thanks. Operations to perform: Apply all…
David J.
  • 1,753
  • 13
  • 47
  • 96
20
votes
5 answers

Adding django admin permissions in a migration: Permission matching query does not exist

I wanted to add some groups and assign permissions to them in a manually written migration but if I run it on a clean DB it creates permissions only after running all migrations. I've found this ticket:…
int_ua
  • 1,646
  • 2
  • 18
  • 32
20
votes
5 answers

Django 1.7 - App 'your_app_name' does not have migrations

I am trying to upgrade from Django 1.6.7 to Django 1.7.1, so I have been trying to migrate my app. I have followed the django docs here. I deleted the south from my installed apps. In the migration directory, I delete the numbered migration files…
user1261774
  • 3,525
  • 10
  • 55
  • 103
19
votes
4 answers

Django makemigrations not detecting project/apps/myapp

Solved Interesting: ./manage.py makemigrations apps.myapp -> "App 'apps.myapp' could not be found. Is it in INSTALLED_APPS?" ./manage.py makemigrations myapp -> Works. ./manage.py makemigrations -> "No changes detected" (it does detect changes if…
danbal
  • 1,481
  • 3
  • 14
  • 19
19
votes
2 answers

How to merge consecutive database migrations in django 1.9+?

Migrations allow transforming from one database schema to another while maintaining current data in the database. Django allows creating migrations using the command python manage.py makemigrations Each time makemigrations is run a new migration…
Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99