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

Cleanest way to delete stale ContentTypes?

I'd like to get rid of the follow message I get when running migrations: The following content types are stale and need to be deleted: appname | modelname Any objects related to these content types by a foreign key will also be deleted. Are…
jamjar
  • 625
  • 5
  • 13
11
votes
2 answers

Automatic db router for migrations in django

I am building a Django powered site and I want to have separate databases for some of the apps, I build a flexible router that routes each app to predefined database, this works fine. The problem is that when I am migrating my models I have to set…
Raff89
  • 403
  • 3
  • 11
11
votes
1 answer

How to properly make migrations when adding a new unique field

I added a new field to one of my models: class Agency(models.Model): email = models.EmailField(unique=True, verbose_name=_("e-mail")) As this field cannot be blank, django-admin makemigrations requested me to provide one-off default, which I…
Antoine Pinsard
  • 33,148
  • 8
  • 67
  • 87
11
votes
2 answers

Django Migration Error - NodeNotFoundError

Django verstion 1.8 Trying to migrate a newly added app in my project. Here is the traceback error: Traceback (most recent call last): File "./manage.py", line 10, in execute_from_command_line(sys.argv) File…
Nicoale
  • 503
  • 2
  • 10
  • 23
11
votes
1 answer

Deleting unused Models, stale content types prompt

I am removing an unnecessary table and model from our Django website. I have removed all foriegn key references before the migrations.DeleteModel(...) is called, but I still am receiving the following prompt when I run the migration: The following…
Nathan Tregillus
  • 6,006
  • 3
  • 52
  • 91
11
votes
6 answers

Can I delete the django migration files inside migrations directory

I personally like django for its MVC ideals. But while i am running Django migrations in version 1.7 each and every migrations i do in it is stored inside the migrations directory. If i delete those file it is throwing an error while migration. I…
sathya Narrayanan
  • 499
  • 1
  • 5
  • 16
11
votes
1 answer

Change column type with django migrations

Consider this structure: some_table(id: small int) and I want change it to this: some_table(id: string) Now I do this with three migrations: Create a new column _id with type string (datamigration) Copy data from id to _id with string…
kharandziuk
  • 12,020
  • 17
  • 63
  • 121
10
votes
4 answers

Conditional Django migration based on a field only present in new version

My app that currently depends on Postgres and Django's Postgres-only JSONField. The field works well and I've no interest in another project, but I have prospective-users who want to use my app, but can't while it relies on Postgres. Django 3.1 has…
Oli
  • 235,628
  • 64
  • 220
  • 299
10
votes
2 answers

How to see SQL query that caused error in Django migration?

I'm trying to run a data migration that deletes all rows in a table (say, MyModel). There is another table that points to that table (RelatedModel). The field in the RelatedModel that maps to MyModel has on_delete=models.SET_NULL. When I run the…
Ariel
  • 3,383
  • 4
  • 43
  • 58
10
votes
6 answers

ValueError in Django when running the "python manage.py migrate" command

I needed to add more fields to Django's User model, so I created a custom model class (named Accounts in an app named accounts) that extends Django's AbstractUser class. After that, I updated my settings.py file, defining the AUTH_USER_MODEL…
Inyavic Sage
  • 145
  • 1
  • 2
  • 11
10
votes
2 answers

Django 1.9 with CORS dumping data: "corsheaders_corsmodel" does not exist

I'm developing a Django (1.9) Rest backend and AngularJS frontend with Cross-site referencing. While attempting to execute a ./manage.py dumpdata command, it throws the following exception: $ python manage.py dumpdata -o…
user3897818
  • 101
  • 3
10
votes
2 answers

Django migrations reference a deleted module

I have a Model named FooModel defined in a my_app/models/foo.py. After deleting foo.py, running Django (1.7) migrations raises an error since the old migration files import foo.py (import myapp.models.foo.FooModel). How should I resolve this? This…
eugene
  • 39,839
  • 68
  • 255
  • 489
10
votes
3 answers

Django 1.7.3 - Lookup failed for model referenced by field

I am trying create a new model with Django, but I keep running into the error Lookup failed for model referenced by field help.HelpDefinition.org: account.Organization. Organization has been imported. You can see the model below. models.py org =…
RHPT
  • 2,560
  • 5
  • 31
  • 43
10
votes
2 answers

Alter model to add "through" relationship to order a ManytoMany field - Django 1.7 migration modification

I am trying to add an order to a ManyToMany field that I created a while ago. I basically want to order pictures in collections of pictures. I am running on Django 1.7, so no more South migrations (I was trying to follow this tutorial:…
Alb Dum
  • 1,121
  • 3
  • 11
  • 26
10
votes
3 answers

Specify app dependency in migration

I'm trying to add initial data in Django 1.7 and I've read that it is recommended to use data migrations. I've created my migration file correctly, called "0001_groups", in which I create few contrib.auth's groups and permissions. The problem is…
Shoe
  • 74,840
  • 36
  • 166
  • 272