Place to set Sqlite PRAGMA option in Django project

后端 未结 2 549
小蘑菇
小蘑菇 2021-02-04 11:45

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 t

相关标签:
2条回答
  • 2021-02-04 11:50

    The article suggests that you add that as a middleware (at the very end). That middleware is then configured as a string inside settings.py, so you shouldn't get any import conflicts.

    0 讨论(0)
  • 2021-02-04 12:01

    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)
    
    0 讨论(0)
提交回复
热议问题