Creating Raw SQL Migrations in Django

I am using Django, which has great support for PostgreSQL-specific functions in it’s ORM. I wanted to use a SearchVectorField inside my application and PostgreSQL recommends creating a trigger for to populate that column. Well that was easy enough but I didn’t want to have a snippet of SQL laying around to have to remember to put that in the database manually. Django has a great migration system – so why not use that? Can we manage a raw SQL entry in a Django migration?

It seems that you can, but no where spells it out step-by-step. I combined a few posts and Django documentation together and figured out how to create the trigger manually. Note that I didn’t make a backwards migration for it – I assumed if I didn’t need this after all, I will remove it myself or write another migration.

First step is to create an empty migration. This is simple: ./manage makemigration <myapp> --empty. It will tell you the file of the empty migration file.

Open that file in your editor, and it will look something like the file below. The entry in the dependencies line will be different based on your app name and what your previous migration was.

from django.db import connection
from django.db import migrations
 
class Migration(migrations.Migration):
 
    dependencies = [
        ('myapp, '0012_auto_20190122_1540'),
    ]
 
    operations = [
 
    ]

Now we need to write a function that will actually run the SQL that we need. Just put that function above the Migration class in the file. Here was mine.

def make_trigger(apps,schema_editor):
 
   with connection.cursor() as cursor:
      cursor.execute("""CREATE TRIGGER caretaker_tsvectorupdate BEFORE INSERT OR UPDATE
ON myapp_myobject FOR EACH ROW EXECUTE procedure
tsvector_update_trigger(search_vector, 'pg_catalog.english', search_txt);
""")

The cursor will execute our command to create our trigger. Of course, you can put any SQL command you want in there.

Now you just need to tell the migration to run that function. That turns out to be a one-liner in the Operations list. The final migration file looked list this:

from django.db import connection
from django.db import migrations
 
def make_trigger(apps,schema_editor):
 
   with connection.cursor() as cursor:
      cursor.execute("""CREATE TRIGGER caretaker_tsvectorupdate BEFORE INSERT OR UPDATE
ON units_caretaker FOR EACH ROW EXECUTE procedure
tsvector_update_trigger(search_vector, 'pg_catalog.english', search_txt);
""")
 
class Migration(migrations.Migration):
 
    dependencies = [
        ('units', '0012_auto_20190122_1540'),
    ]
 
    operations = [
       migrations.RunPython(make_trigger)
 
    ]

Now just run the migration: ./manage.py migrate myapp and now your SQL will be ran in your existing table.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*