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
5 answers

Django: How to migrate from ManyToMany to ForeignKey?

I'm building a REST API and server using Django and Django Rest Framework. We are using a postgres database. I need to simplify a badly designed relation. We have a model (House) that has a ManyToMany relationship to another (City). In reality it…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
9
votes
1 answer

Django / makemigrations ModuleNotFoundError: No module named 'idmp_core.apps.IdmpCoreConfigdjango';

I'm learning Django... And there is an error I'm not able to fix when running makemigrations command. I got the error ModuleNotFoundError: No module named 'idmp_core.apps.IdmpCoreConfigdjango'; 'idmp_core.apps' is not a package. What is puzzling me…
9
votes
1 answer

How to prevent Django 1.11 from creating migrations for unmanaged models?

I have a Django 1.11.10 project with an unmanaged model like: class MyModel(models.Model): id = models.PositiveIntegerField(primary_key=True) name = models.CharField(max_length=500) class Meta: managed = False The model wraps…
Cerin
  • 60,957
  • 96
  • 316
  • 522
9
votes
1 answer

How to run Django migrations in Google App Engine Flexible deployment step?

I have a Django app up and running in Google App Engine flexible. I know how to run migrations using the cloud proxy or by setting the DATABASES value but I would like to automate running migrations by doing it in the deployment step. However, there…
Alex
  • 126
  • 1
  • 5
9
votes
3 answers

Django Migrations: Same migrations being created with makemigrations

Django is creating the same migrations file repeatedly when calling: ./manage.py makemigrations The same migrations will be created in a new migrations file every time makemigrations is run regardless of if the changes are migrated or not. The…
9
votes
2 answers

How to migrate django custom base64 field. Field does not exist

I have a base64 field that is copied from the django snippet. https://djangosnippets.org/snippets/1669/ class Base64Field(models.TextField): """ https://djangosnippets.org/snippets/1669/ Example use: class Foo(models.Model): …
Akamad007
  • 1,551
  • 3
  • 22
  • 38
9
votes
2 answers

Why are my Django migrations loading my urls.py?

I'm trying to run django migrations on a freshly installed Heroku instance but was getting a ProgrammingError. The error was due to some module-level queries that were being performed in a totally separate module and shouldn't be called at all…
Soviut
  • 88,194
  • 49
  • 192
  • 260
9
votes
1 answer

Model not found in migration (apps.get_model raises LookupError)

I have the following migration (logic removed for simplicity): def migrate_existing_discounts(apps, _): ModelA = apps.get_model('myapp', 'ModelA') ModelB = apps.get_model('myapp', 'ModelB') class Migration(migrations.Migration): …
dnaranjo
  • 3,647
  • 3
  • 27
  • 41
9
votes
2 answers

How to convert django model to abstract model if it already has related classes

Lets say i have the following base model: class human(models.Model): gender = models.BooleanField() age = models.IntegerField() name = models.CharField(max_length=200) And two models inheriting it: class superhero(human): can_fly =…
George
  • 988
  • 2
  • 10
  • 25
9
votes
3 answers

Detect whether code is being run in the context of migrate/makemigrations command

I have a model with dynamic choices, and I would like to return an empty choice list if I can guarantee that the code is being run in the event of a django-admin.py migrate / makemigrations command to prevent it either creating or warning about…
DanH
  • 5,498
  • 4
  • 49
  • 72
9
votes
2 answers

How do I disable django migration debug logging?

Very similar to lafagundes question about south migration debug logging, except I'm not using south - I'm using plain Django 1.7 migrations. I'm also using django-nose test runner. When I run manage.py test, there is no debug logging output…
groovecoder
  • 1,551
  • 1
  • 17
  • 27
9
votes
8 answers

KeyError: ('profiles', 'talk') - How do I resolve?

Newbie here. Trying to build an app using Django and Postgres db. I am struggling to migrate at the moment getting this error "KeyError: ('profiles', 'talk')" Here is my error from my command line after trying to migrate: (myvenv) Abbys-iMac:talks…
bibby3316
  • 103
  • 1
  • 1
  • 6
9
votes
3 answers

Django migrations fail in heroku

I'm trying to deploy a Django (1.8) app to Heroku, runtime Python 3.4.2. The app runs succesfully (except the fact that tables are not created), but when trying run the syncdb I get the following error: Running `python manage.py syncdb` attached to…
user3706162
  • 127
  • 3
  • 6
9
votes
3 answers

Django data migration fails when running manage.py test, but not when running manage.py migrate

I have a Django 1.7 migration that looks something like this: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations def units_to_m2m(apps, schema_editor): Interval = apps.get_model("myapp",…
9
votes
1 answer

Provide a default for ForeignKey field on existing entries in Django

I have this model: class Movie(models.Model): # here are some fields for this model to which I added the following field (the database was already populated with Movie models): user = models.ForeignKey(User, default=1) I ran the commands…
Kaladin11
  • 195
  • 1
  • 2
  • 7