16π
There are AddIndexConcurrently
and RemoveIndexConcurrently
in Django 3.0:
Create a migration and then change migrations.AddIndex
to AddIndexConcurrently
. Import it from django.contrib.postgres.operations
.
13π
With Django 1.10 migrations you can create a concurrent index by using RunSQL
and disabling the wrapping transaction by making the migration non-atomic by setting atomic = False
as a data attribute on the migration:
class Migration(migrations.Migration):
atomic = False # disable transaction
dependencies = []
operations = [
migrations.RunSQL('CREATE INDEX CONCURRENTLY ...')
]
- [Django]-How to server HTTP/2 Protocol with django
- [Django]-How do I deploy Django on AWS?
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
10π
You could use the SeparateDatabaseAndState
migration operation to provide a custom SQL command for creating the index. The operation accepts two lists of operations:
-
state_operations are operations to apply on the Django model state.
They do not affect the database. -
database_operations are operations to apply to the database.
An example migration may look like this:
from django.db import migrations, models
class Migration(migrations.Migration):
atomic = False
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
# operation generated by `makemigrations` to create an ordinary index
migrations.AlterField(
# ...
),
],
database_operations=[
# operation to run custom SQL command (check the output of `sqlmigrate`
# to see the auto-generated SQL, edit as needed)
migrations.RunSQL(sql='CREATE INDEX CONCURRENTLY ...',
reverse_sql='DROP INDEX ...'),
],
),
]
- [Django]-Why there are two process when i run python manage.py runserver
- [Django]-How about having a SingletonModel in Django?
- [Django]-How to make two django projects share the same database
1π
Do what tgroshon says for new django 1.10 +
for lesser versions of django i have had success with a more verbose subclassing method:
from django.db import migrations, models
class RunNonAtomicSQL(migrations.RunSQL):
def _run_sql(self, schema_editor, sqls):
if schema_editor.connection.in_atomic_block:
schema_editor.atomic.__exit__(None, None, None)
super(RunNonAtomicSQL, self)._run_sql(schema_editor, sqls)
class Migration(migrations.Migration):
dependencies = [
]
operations = [
RunNonAtomicSQL(
"CREATE INDEX CONCURRENTLY",
)
]
- [Django]-Django edit user profile
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-How do I use django rest framework to send a file in response?
0π
There is no support for PostgreSQL concurent index creation in django.
Here is the ticket requesting this feature β https://code.djangoproject.com/ticket/21039
But instead, you can manually specify any custom RunSQL operation in the migration β
https://docs.djangoproject.com/en/stable/ref/migration-operations/#runsql
- [Django]-CORS error while consuming calling REST API with React
- [Django]-Django/DRF β 405 Method not allowed on DELETE operation
- [Django]-Removing 'Sites' from Django admin page
0π
You can do something like
import django.contrib.postgres.indexes
from django.db import migrations, models
from django.contrib.postgres.operations import AddIndexConcurrently
class Migration(migrations.Migration):
atomic = False
dependencies = [
("app_name", "parent_migration"),
]
operations = [
AddIndexConcurrently(
model_name="mymodel",
index=django.contrib.postgres.indexes.GinIndex(
fields=["field1"],
name="field1_idx",
),
),
AddIndexConcurrently(
model_name="mymodel",
index=models.Index(
fields=["field2"], name="field2_idx"
),
),
]
- [Django]-Django 1.7 β App 'your_app_name' does not have migrations
- [Django]-How to run celery as a daemon in production?
- [Django]-Difference between User.objects.create_user() vs User.objects.create() vs User().save() in django