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
88
votes
11 answers

Check for pending Django migrations

In Django, is there an easy way to check whether all database migrations have been run? I've found manage.py migrate --list, which gives me the information I want, but the format isn't very machine readable. For context: I have a script that…
Moss Collum
  • 3,542
  • 3
  • 25
  • 23
79
votes
4 answers

Django migration with uuid field generates duplicated values

I have a uuid field (not a primary key). The generated migration is: from __future__ import unicode_literals from django.db import migrations, models import uuid class Migration(migrations.Migration): dependencies = [ .... ] …
blueFast
  • 41,341
  • 63
  • 198
  • 344
78
votes
10 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,806
  • 10
  • 33
  • 31
77
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,369
  • 6
  • 26
  • 33
76
votes
7 answers

Django migrations RunPython not able to call model methods

I'm creating a data migration using the RunPython method. However when I try to run a method on the object none are defined. Is it possible to call a method defined on a model using RunPython?
user2954587
  • 4,661
  • 6
  • 43
  • 101
68
votes
2 answers

Django data migration when changing a field to ManyToMany

I have a Django application in which I want to change a field from a ForeignKey to a ManyToManyField. I want to preserve my old data. What is the simplest/best process to follow for this? If it matters, I use sqlite3 as my database back-end. If…
Ken H
  • 681
  • 1
  • 5
  • 3
67
votes
2 answers

How would you create a 'manual' django migration?

I've discovered that I can set defaults for columns on a postgres database in a django project using migrations.RunSQL('some sql'). I'm currently doing this by adding a column, makemigrations, then removing the column, makemigrations, and then…
Keith John Hutchison
  • 4,955
  • 11
  • 46
  • 64
67
votes
3 answers

Rerun a Django data migration

How would I rerun a data migration on Django 1.8+? If relevant, my migration is numbered 0011_my_data_migration.py and is the latest migration.
davidhwang
  • 1,343
  • 1
  • 12
  • 19
65
votes
4 answers

ValueError: Dependency on app with no migrations: customuser

I am trying to start a webpage using the Django framework. This is my first web development project. After creating the project, I tried to start an app that utilizes customized users and registration with email validation using…
63
votes
5 answers

Can I use a database view as a model in Django?

i'd like to use a view i've created in my database as the source for my django-view. Is this possible, without using custom sql? ******13/02/09 UPDATE*********** Like many of the answers suggest, you can just make your own view in the database and…
spence91
  • 77,143
  • 9
  • 27
  • 19
63
votes
5 answers

'NOT NULL constraint failed' after adding to models.py

I'm using userena and after adding the following line to my models.py zipcode = models.IntegerField(_('zipcode'), max_length=5) I get the following error after I hit the submit button on th signup…
stephan
  • 2,293
  • 4
  • 31
  • 55
59
votes
18 answers

"No installed app with label 'admin'" running Django migration. The app is installed correctly

I am trying to use admin.LogEntry objects during a datamigration on Django 1.7 The 'django.contrib.admin' app is listed on INSTALLED_APPS. On the shell, it works: >>> from django.apps import apps >>> apps.get_model('admin',…
alanjds
  • 3,972
  • 2
  • 35
  • 43
56
votes
5 answers

Django "You have unapplied migrations". Which ones?

Django runserver complains: You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. How can I find out which migrations are unapplied without running migrate?
Pier1 Sys
  • 1,250
  • 2
  • 12
  • 21
55
votes
12 answers

Django manage.py: Migration applied before its dependency

When running python manage.py migrate I encounter this error: django.db.migrations.exceptions.InconsistentMigrationHistory: Migration .0016_auto__ is applied before its…
51
votes
2 answers

How do I execute raw SQL in a django migration

I am aware of the cursor object in Django. Is there any other preferred way to execute raw SQL in migrations? I want to introduce postgresql partitioning for one of my models tables. The partition logic is a bunch of functions and triggers that have…
David Schumann
  • 13,380
  • 9
  • 75
  • 96
1
2
3
97 98