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

Is it safe to rename Django migrations file?

Since Django 1.8 the makemigrations command has a --name, -n option to specify custom name for the created migrations file. I'd like to know whether it's safe in older versions of Django to create the migrations file with the automatically…
geckon
  • 8,316
  • 4
  • 35
  • 59
50
votes
8 answers

Getting model ContentType in migration - Django 1.7

I have a data migration that updates some permissions. I know there are some known issues with permissions in migrations and i was able to avoid some trouble by creating the permissions in the migration it self (rather then using the tuple shortcut…
haki
  • 9,389
  • 15
  • 62
  • 110
49
votes
17 answers

Django Programming error column does not exist even after running migrations

I run python manage.py makemigrations and I get: No changes detected Then, python manage.py migrate and I get: No migrations to apply. Then, I try to push the changes to production: git push heroku master Everything up-to-date Then,…
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
48
votes
1 answer

How to redo a migration on django 1.8 after using --fake

Something went wrong on my migrations, I added a new datetimefield to a model then I used makemigrations and migrate. python manage.py makemigrations python manage.py migrate But after this the migrate got an "table already exists error". I…
Fernando Freitas Alves
  • 3,709
  • 3
  • 26
  • 45
46
votes
8 answers

ValueError: Related model u'app.model' cannot be resolved

I have two applications (ook and eek say) and I want to use a foreign key to a model in ook from a model in eek. Both are in INSTALLED_APPS with ook first. In ook.models.py, i have: class Fubar(models.Model): ... In eek.models.py, I have: class…
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156
45
votes
2 answers

django 1.7 migrations -- how do I clear all migrations and start over from scratch?

So I'm rapidly iterating on a django app at the moment and I'm constantly adjusting models.py. Over the course of a day or two of programming and testing I generate a couple dozen migration files. Sometimes I really tear the schema apart and…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
41
votes
5 answers

How does django know which migrations have been run?

How does django know whether a migration has been applied yet? It usually gets it right, but when it doesn't I don't ever know where to start troubleshooting.
BostonJohn
  • 2,631
  • 2
  • 26
  • 48
41
votes
4 answers

How to add a new field to a model with new Django migrations?

I'm using the contribute_to_class method but I don't know how to create the field in the database with new migrations.
pedro_witoi
  • 459
  • 1
  • 5
  • 4
40
votes
11 answers

Migrating from django user model to a custom user model

I am following these two references (one and two) to have a custom user model in order to authenticate via email and also to add an extra field to it. class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField( …
Kakar
  • 5,354
  • 10
  • 55
  • 93
40
votes
4 answers

Django - Cannot create migrations for ImageField with dynamic upload_to value

I just upgraded my app to 1.7 (actually still trying). This is what i had in models.py: def path_and_rename(path): def wrapper(instance, filename): ext = filename.split('.')[-1] # set filename as random string filename =…
alioguzhan
  • 7,657
  • 10
  • 46
  • 67
39
votes
17 answers

InvalidBasesError: Cannot resolve bases for []

When I run tests I get this error during database initialization: django.db.migrations.state.InvalidBasesError: Cannot resolve bases for [] This can happen if you are inheriting models from an app with migrations…
39
votes
2 answers

Django 1.7.1 Makemigrations fails when using lambda as default for attribute

I'm using Django 1.7.1. My model looks like this: from datetime import datetime from django.db import models class myModel(models.Model): x = models.CharField(max_length=254,null=True, blank=True,) Everything works perfectly fine. However,…
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
37
votes
9 answers

Received "ValueError: Found wrong number (0) of constraints for ..." during Django migration

While using Django 1.7 migrations, I came across a migration that worked in development, but not in production: ValueError: Found wrong number (0) of constraints for table_name(a, b, c, d) This is caused by an AlterUniqueTogether rule: …
rrauenza
  • 6,285
  • 4
  • 32
  • 57
37
votes
2 answers

Why does django 1.7 creates migrations for changes in field choices?

I have observed this behaviour on version 1.7 but not in previous versions using south migration. eg. class RedemptionCode(models.Model): EXPIRE_OPTIONS = ( ('1 week', '1 Week'), ) expire_option =…
James Lin
  • 25,028
  • 36
  • 133
  • 233
36
votes
2 answers

How can I skip a migration with Django migrate command?

First, I am asking about Django migration introduced in 1.7, not south. Suppose I have migrations 001_add_field_x, 002_add_field_y, and both of them are applied to database. Now I change my mind and decide to revert the second migration and replace…
NeoWang
  • 17,361
  • 24
  • 78
  • 126
1 2
3
97 98