0%

Django:在Migrations中执行原生SQL

First, generate a new empty migration:

1
2
3
$ python manage.py makemigrations app --empty --name add_index_runsql
Migrations for 'app':
app/migrations/0002_add_index_runsql.py

Next, edit the migration file and add a RunSQL operation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# migrations/0002_add_index_runsql.py

from django.db import migrations, models

class Migration(migrations.Migration):
atomic = False

dependencies = [
('app', '0001_initial'),
]

operations = [
migrations.RunSQL(
'CREATE INDEX app_sale_sold_at_b9438ae4 ON app_sale (sold_at);',
reverse_sql='DROP INDEX "app_sale_sold_at_b9438ae4";',
),
]

When you run the migration, you will get the following output:

1
2
3
4
5
$ python manage.py migrate
Operations to perform:
Apply all migrations: app
Running migrations:
Applying app.0002_add_index_runsql... OK

migrate back to the initial migration:

1
2
3
4
5
6
$ python manage.py migrate 0001
Operations to perform:
Target specific migration: 0001_initial, from app
Running migrations:
Rendering model states... DONE
Unapplying app.0002_add_index_fake... OK