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
6
votes
1 answer

Django: revert merge migration

Let's suppose we have migrations with the following dependency graph (all applied): Initial state Now, for some reason we want to revert database schema to the state after applying migration 0006_f. We type: ./manage.py migrate myapp 0006_f and…
6
votes
2 answers

Django migration default value callable generates identical entry

I am adding a new field to an existing db table. it is to be auto-generated with strings. Here is my code: from django.utils.crypto import get_random_string ... Model: verification_token = models.CharField(max_length=60, null=False,…
Justin M. Ucar
  • 853
  • 9
  • 17
6
votes
3 answers

How to setup django migrations to be always the last one applied

I have django application and migration file 002_auto.py, which uses Django RunPython to alter database. I have no idea how much migrations files would be created in future, but I want the file 002_auto.py to be applied as the last part of migration…
Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
6
votes
1 answer

Django 1.9 can't modify unique_together (ValueError) wrong number of constrains

I created my models using inspectdb, it struck me odd at the time that django generated the following since there is just one such index, but i let it be unique_together = (('p_id', 'v_id', 'sku',), ('p_id', 'v_id', 'sku',)) now i'm trying to add…
jayshilling
  • 331
  • 2
  • 4
  • 11
6
votes
1 answer

How to switch the direction of a Django OneToOneField?

I currently have a couple of models set up like this: from django.db import models class Child(models.Model): child_name = models.CharField(max_length=200) class Parent(models.Model): parent_name = models.CharField(max_length=200) …
clwainwright
  • 1,624
  • 17
  • 21
6
votes
4 answers

How to remove index varchar_pattern_ops in a django (1.8) migration?

When creating a model with a models.varchar(...) field, a varchar_pattern_ops index is being created. This is the table generated in postgresql Table "public.logger_btilog" Column | Type | Modifiers…
jperelli
  • 6,988
  • 5
  • 50
  • 85
6
votes
6 answers

Django 1.8 migrate: django_content_type does not exist

I just upgraded my django from 1.7.1 to 1.8.4. I tried to run python manage.py migrate but I got this error: django.db.utils.ProgrammingError: relation "django_content_type" does not exist I dropped my database, created a new one, and ran the…
AliBZ
  • 4,039
  • 12
  • 45
  • 67
6
votes
1 answer

Django "changes not reflected in a migration" with ImageField and custom storage

My Django 1.8 app uses a third-party app (django-avatar) whose model contains an ImageField. I'm also using a custom DEFAULT_FILE_STORAGE (S3BotoStorage from django-storages-redux) in my project's settings.py. As a result, every time I run manage.py…
medmunds
  • 5,950
  • 3
  • 28
  • 51
6
votes
4 answers

Django Migrations Says Database Backend Isn't Available

I'm trying to upgrade a Django 1.6.2 application to 1.7.10. I'm running PostgreSQL on my Mac using the Postgres.app version 9.3.4 that runs PostgreSQL 9.3.4 and PostGIS 2.1.1. The problem I'm having is that when I run the new "makemigrations"…
Jim
  • 13,430
  • 26
  • 104
  • 155
6
votes
5 answers

Django 1.8 app mysteriously fails with initial migration due to errno: 150 "Foreign key constraint is incorrectly formed"

I'm writing an app in django 1.8 with python 3.4 and I'm encountering an issue with using MySQL as the database backend which has got me completely stumped. When I start off with a new database and call ./manage.py migrate (or syncdb) and it tries…
Tobias
  • 924
  • 9
  • 23
6
votes
2 answers

Why doesn't django dumpdata include the django_migrations table?

Django dumpdata (with no app specified) dumps all the tables of all the installed apps to an output file. I just realized this did not include the django_migrations table. I checked the other django tables, they were included as they were specified…
mehmet
  • 7,720
  • 5
  • 42
  • 48
6
votes
2 answers

How to fake migrations for not to create a specific existing intermediary table

I have following models class VucutBolgesi(models.Model): site = models.ForeignKey(Site) bolge = models.CharField(verbose_name="Bölge", max_length=75) hareketler = models.ManyToManyField("Hareket", verbose_name="Hareketler", null=True,…
Mp0int
  • 18,172
  • 15
  • 83
  • 114
6
votes
3 answers

django makemigrations with python-social-auth leads to permission denied error

After adding python social auth to my installed apps, i.e. INSTALLED_APPS = ( ... 'social.apps.django_app.default', ... ) and then trying a python manage.py makemigrations I get an unsurprising permissions error Migrations for…
mjandrews
  • 2,392
  • 4
  • 22
  • 39
6
votes
1 answer

Django: Migrations depend on removed 3rd-party module

In my django project, I have been using django-taggit to add tagging capabilities to a model. The migration adding tags also lists the initial taggit migration as dependency: dependencies = [ ('taggit', '0001_initial'), # … ] At a later…
David Aurelio
  • 484
  • 2
  • 11
6
votes
1 answer

Cannot create an instance of a model with GenericForeignKey in migration

IMPORTANT: This question is no longer relevant. In a Django 1.7 migration I try to create Comment entries programatically with the following code: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models,…
Paweł Kłeczek
  • 603
  • 1
  • 5
  • 28