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
16
votes
8 answers

django: migration x in app x has no Migration class

here is the exact error django.db.migrations.loader.BadMigrationError: Migration 0001_initial in app django_comments has no Migration class I have no idea what this means and I don't know where to go. I did some work with my venv, making a new one…
Joff
  • 11,247
  • 16
  • 60
  • 103
16
votes
4 answers

django.core.exceptions.FieldDoesNotExist: model has no field named

After some googling and only finding a dead-end topic, I'm still stuck on a migration problem. My model : class CurationArticle(models.Model): title = models.CharField(max_length=150, null=True, blank=True) description =…
Raphaël Gomès
  • 938
  • 1
  • 8
  • 23
16
votes
3 answers

How to execute code on post_migrate signal in Django?

I'm doing some kind of refactoring for my project, where I'm relying on the django django.contrib.auth.models.Permission model. So far I define the permissions for each new user using a post_save signal, so when the user is created, I assign their…
slackmart
  • 4,754
  • 3
  • 25
  • 39
16
votes
3 answers

Remove app with Django 1.7 migrations

I would like to know whats the cleanest way to remove all tables for a removed app using Django migrations. If for example I install a new package, I add the app to my settings.py and I do a manage.py makemigrations and a manage.py migrate; when…
diegopau
  • 1,324
  • 10
  • 22
15
votes
1 answer

Error Using CheckConstraint in Model.Meta along with Django GenericForeignKey - Joined field references are not permitted in this query

I am trying to restrict GFK to be pointed to objects of a few models only, and I thought CheckConstraint will be a great way to do this, however I get this error class ManualAdjustment(Model): content_type = models.ForeignKey(ContentType,…
15
votes
4 answers

django.db.migrations.exceptions.CircularDependencyError

I have a problem with Django migrations on empty DB. When I want to migrate I have a circular dependency error. Circular dependency error between two apps that related by foreign keys /firstapp/models.py class Person(models.Model): ... class…
yarsanich
  • 174
  • 1
  • 1
  • 10
15
votes
3 answers

Is it OK to print to stdout or stderr in Django data migrations? If so, how?

I've been looking for any guidelines on this but with no success. In a project I use Django data migrations quite often. They look more-or-less like the example from the docs. However, the operations are sometimes rather complex and it would be nice…
Piotr Ćwiek
  • 1,580
  • 13
  • 19
15
votes
1 answer

Getting a "The following content types are stale and need to be deleted" when trying to do a migrate. What does this mean, and how can I solve it?

This is my models.py: class Notification(models.Model): user = models.ForeignKey(User) createdAt = models.DateTimeField(auto_now_add=True, blank=True) read = models.BooleanField(default=False, blank=True) class Meta: …
SilentDev
  • 20,997
  • 28
  • 111
  • 214
15
votes
2 answers

right way to create a django data migration that creates a group?

I would like to create data migrations that create Permissions and Groups, so that my other developers can just run the migrations and get everything set up. I was able to create the migrations and run them just fine, but now I'm getting an error…
Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
15
votes
2 answers

Django 1.7 blank CharField/TextField convention

Using Django's new migration framework, let's say I have the following model that already exists in the database: class TestModel(models.Model): field_1 = models.CharField(max_length=20) I now want to add a new TextField to the model, so it…
Jeremy Swinarton
  • 517
  • 1
  • 5
  • 14
15
votes
2 answers

Models inside tests - Django 1.7 issue

I'm trying to port my project to use Django 1.7. Everything is fine except 1 thing. Models inside tests folders. Django 1.7 new migrations run migrate command internally. Before syncdb was ran. That means if a model is not included in migrations -…
tunarob
  • 2,768
  • 4
  • 31
  • 48
14
votes
2 answers

Why does Django drop the SQL DEFAULT constraint when adding a new column?

In the latest Django (2.2), when I add a new field to a model like this: new_field= models.BooleanField(default=False) Django runs the following commands for MySQL: ALTER TABLE `app_mymodel` ADD COLUMN `new_field` bool DEFAULT b'0' NOT NULL; ALTER…
Petter
  • 37,121
  • 7
  • 47
  • 62
14
votes
4 answers

django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey"

Got stuck I have an database in which when I try to make python manage.py migrate it's giving this error as follows: django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey" DETAIL: Key (id)=(241)…
Piyush S. Wanare
  • 4,703
  • 6
  • 37
  • 54
14
votes
2 answers

What does "serialize=False" mean on Django's "primary_key" fields in migration files?

I couldn't find the reason for serialize=False being set on primary key fields in either the Django docs or the source code. Is there a special reason to set it? Thanks
Azd325
  • 5,752
  • 5
  • 34
  • 57
14
votes
3 answers

How to get a name of last migration programmatically?

I want to get the name of the last applied migration in Django. I know that Django migrations are stored in django_migrations table, however django.db.migrations.migration.Migration is not a models.Model backed by that table. This means you cannot…
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162