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
19
votes
3 answers

django: exclude models from migrations

In my django application (django 1.8) I'm using two databases, one 'default' which is MySQL, and another one which is a schemaless, read-only database. I've two models which are accessing this database, and I'd like to exclude these two models…
thekorn
  • 207
  • 1
  • 2
  • 4
19
votes
6 answers

Do django db_index migrations run concurrently?

I'm looking to add a multi-column index to a postgres database. I have a non blocking SQL command to do this which looks like this: CREATE INDEX CONCURRENTLY shop_product_fields_index ON shop_product (id, ...); When I add db_index to my model and…
yekta
  • 3,363
  • 3
  • 35
  • 50
19
votes
2 answers

Creating Partial Indexes with Django 1.7

The documentation for Django 1.7 mentions RunSQL classes can be used to create partial indexes on your tables. I have a table where I want the combination of title, blog & category to be unique. However if category is not provided, the combination…
user4150760
  • 2,739
  • 5
  • 18
  • 25
18
votes
6 answers

Django: Safely Remove Old Migrations?

I've got a Django app with a lot of out-of-date migrations. I'd like to remove the old migrations and start fresh. The app has 14 different "migrations" folders. Here is what a few of them look like: Is it safe to remove all the contents from…
VikR
  • 4,818
  • 8
  • 51
  • 96
18
votes
2 answers

Django migrate and makemigrate automatic yes on prompt

Is there a way to automatically specify YES as the default option on manage.py makemigrations myAPp and manage.py migrate commands i tried the --noinput option on migrate but i think it defaults to NO not YES
Dhanushka Amarakoon
  • 3,502
  • 5
  • 31
  • 43
18
votes
3 answers

Django Sites Framework: Initial Data Migration Location

Before Django 1.7, when using the Django Sites Framework one could/should define the initial data using Initial Fixtures. myproject/fixtures/initial_data.json [ { "pk": 1, "model": "sites.site", "fields": { "domain":…
JCJS
  • 3,031
  • 3
  • 19
  • 25
17
votes
2 answers

Fix Conflicting migrations detected in Django1.9

I updated django-dynamic-model repository to support Django 1.9. I got this error: CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0001_initial, 0002_auto__add_field_dynamicschemafield_extra…
Seenu S
  • 3,381
  • 6
  • 30
  • 45
17
votes
2 answers

Django 1.8 Migrations. Adding DateTimeField after db creation. Best practices?

So some time a couple migrations after my first one, I decided I wanted to include these fields: created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) into one of my models. When I makemigrations it gave…
pyramidface
  • 1,207
  • 2
  • 17
  • 39
17
votes
2 answers

Django 1.8 migration unable to cast column id to integer

I'm migrating my site from an SQLite backend to a Postgres backend. We've been running native-Django style migrations (i.e., not South) from the beginning of the project. Most of the migrations run fine, but there's a hiccup in our of our…
Alex H.
  • 563
  • 1
  • 4
  • 21
17
votes
2 answers

How can I send signals from within Django migrations?

I use Django 1.7 migrations, and in particular, want to populate a newly-created database with initial data. Thus, I use a data migration for this. It looks like this: def populate_with_initial_data(apps, schema_editor): User =…
Torsten Bronger
  • 9,899
  • 7
  • 34
  • 41
16
votes
1 answer

Renaming a Django superclass model and updating the subclass pointers correctly

I'm having trouble refactoring a superclass in Django v2.2.12 involving three models, with one superclass model and two subclass models: class BaseProduct(models.Model): name = models.CharField() description = models.CharField() class…
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
16
votes
1 answer

Python 2 -> 3 Django migration causes field parameter type change

We are transitioning a Django project from Django 1.8 -> 2.1 and Python 2.7 -> 3.6. In the old project version, there are Django models that looked like this, for example: # models.py from django.db import models class…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
16
votes
4 answers

django.db.utils.OperationalError: near "[]": syntax error

There is this error showing after I have used makemigrations command I have tried commenting different column for it but it wont work C:\Users\Rushabh\Desktop\project\MyPrj>python manage.py migrate Operations to perform: Apply all migrations:…
rushabh shah
  • 172
  • 1
  • 1
  • 10
16
votes
2 answers

Convert data on AlterField django migration

I have a production database and need to keep safe the data. I want to change a Field in model and convert all data inside that database with this change. Old field class MyModel(models.Model): field_name = models.TimeField() Changed…
16
votes
3 answers

Default value for foreign key in Django migrations.AddField

Using migrations, I need to add a new field (a foreign key) to a model. I know it can be done with: migrations.AddField( model_name='MyModel', name='state', field=models.ForeignKey(null=True, related_name='mymodel_state',…
eelioss
  • 359
  • 1
  • 5
  • 10