Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place to set Sqlite PRAGMA option in Django project

Tags:

sqlite

django

According to this test, setting PRAGMA synchronous=OFF Sqlite can dramatically improve Sqlite write performance.

I am well aware of the drawbacks, but would still like to try this out.

What would be the best location within a Django project to set this PRAGMA option?

I cannot do it from settings.py - at least not the way the article suggests - because from django.db import connection would cause a recursive import error.

like image 928
Tomas Andrle Avatar asked Dec 26 '10 18:12

Tomas Andrle


1 Answers

Add this code in the __init__.py file of one of your installed app:

from django.db.backends.signals import connection_created
def activate_foreign_keys(sender, connection, **kwargs):
    """Enable integrity constraint with sqlite."""
    if connection.vendor == 'sqlite':
        cursor = connection.cursor()
        cursor.execute('PRAGMA foreign_keys = ON;')

connection_created.connect(activate_foreign_keys)
like image 69
Thibault J Avatar answered Sep 21 '22 22:09

Thibault J