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

Django migration: making a row not primary key, getting "ProgrammingError: multiple default values"?

I am working with Postgres on a Django app and I want to make a model change, resetting a row so it is no longer the primary key. This is what my model currently looks like in Django: class Meeting(TimeStampedModel): row =…
Richard
  • 62,943
  • 126
  • 334
  • 542
9
votes
2 answers

How to set default column value in the db using django 1.7.3/postgres migrations?

It seems like default value on columns is only on the ORM layer, and is not actually setting a default value in the DB. At the same time, the ID key for example has a default modifier in the database, which tells me it is possible to do that, but…
Isaac
  • 2,701
  • 4
  • 30
  • 47
8
votes
0 answers

When is it required to use `schema_editor.connection.alias` in a django `migrations.RunPython` method?

When is it required to use schema_editor.connection.alias in a migrations.RunPython method and why? When using RunPython in a data migration, django advises that you should use schema_editor.connection.alias: from django.db import migrations def…
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
8
votes
2 answers

Django abstract model + DB migrations: tests throw "cannot ALTER TABLE because it has pending trigger events"

I want to write an abstract model mixin, that I can use to make OneToOne - relations to the user model. Here is my code: from django.conf import settings from django.db import models class Userable(models.Model): user = models.OneToOneField( …
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
8
votes
2 answers

Automatically generate custom migrations in Django

Django provides a really nice feature called makemigrations where it will create migration files based on the changes in models. We are developing a module where we will want to generate custom migrations. I haven't found much info about creating…
Robbietjuh
  • 848
  • 1
  • 8
  • 22
8
votes
1 answer

Is it possible to change the 'migrations' folder's location outside of the Django project?

what i'm trying to do is to change the default path for migrations for a specific application in a django project to put it outside the project itself but keeping it transparent, keeping use of makemigrations and migrate. Is it possible? if yes,…
Giuseppe
  • 363
  • 5
  • 19
8
votes
2 answers

How do I handle migrations as a Django package maintainer?

I have been writing a new Django package that will be pip-installable. I've been stuck for awhile because I'm unsure how to make migrations for my particular package so to allow for the normal workflow of an install to be: pip install my…
mattjegan
  • 2,724
  • 1
  • 26
  • 37
8
votes
4 answers

Fix 'column already exists' Django Migration Error?

I'm getting a "column of relation already exists" error when I try to run the Django migrate command: Operations to perform: Synchronize unmigrated apps: signin, django_rq, gis, staticfiles, admindocs, messages, pipeline, test_without_migrations,…
Jim
  • 13,430
  • 26
  • 104
  • 155
8
votes
7 answers

How to trace this? AttributeError: 'NoneType' object has no attribute 'is_relation' during makemigrations

I'm getting a confusing error for the second time since yesterday. Last time I just flattened my whole migrations, but I've never actually found what caused the problem. So this comes up when I try to makemigrations for my python project. Where…
JasonTS
  • 2,479
  • 4
  • 32
  • 48
8
votes
3 answers

Django - changing validator name causes traceback in migration

i use Django 1.9.2 with python 3.4.2. In the first half of the development lifecycle i had this code: class ModificationOrder(ERN): ... san_amount = models.IntegerField(default=0, \ validators=[validate_modificationorder_san_amount]) , and…
user2194805
  • 1,201
  • 1
  • 17
  • 35
8
votes
0 answers

Django - Migrate ManyToMany to through

I have a model that looks like this: class Assignment(models.Model): """An assignment covers a range of years, has multiple coders and one specific task""" title = models.CharField(blank=True, max_length=100) start_date =…
LukasKawerau
  • 1,071
  • 2
  • 23
  • 42
8
votes
6 answers

Migration clashes with forms.py

The command python manage.py makemigrations fails most of time due to the forms.py, in which new models or new fields are referenced at class definition level. So I have to comment each such definitions for the migration to operate. It's a painfull…
albar
  • 3,020
  • 1
  • 14
  • 27
8
votes
2 answers

Removing a Django migration that depends on custom field from an old module

I have a Django 1.8 application whose initial migration relies on django-interval-field like this: import interval.fields migrations.CreateModel( name='Item', fields=[ ... ('estimated_time',…
nnyby
  • 4,748
  • 10
  • 49
  • 105
8
votes
3 answers

Why there is need to push django migrations to version control system

This is a common practice that people working on django project usually push migrations to the version control system along with other code. My question is why this practice is so common? Why not just push the updated models and everyone generate…
Ishtiaq
  • 980
  • 2
  • 6
  • 21
8
votes
2 answers

Django: "Unknown Column" when run makemigrations on an app after adding an Integer Field

First of all, I have recently upgraded from Django 1.6 to 1.8, and never used South, so I'm new to migrations. I just ran: > python manage.py makemigrations myapp > python manage.py migrate --fake initial to create the initial migrations for my…