From c8ff48a49687dc7417ac42a7f30b19fe0228c296 Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Sat, 30 Nov 2013 12:47:17 +0100 Subject: [PATCH] Enable Foreign Key support on sqlite --- README.md | 7 ++++--- dusken/__init__.py | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 347fb70..4768fec 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,8 @@ A similar data model might allready [exist](http://www.databaseanswers.org/data_ * [Tastypie](https://django-tastypie.readthedocs.org/) ## SQLite3 vs PostgreSQL -Tests run much faster with SQLite, but it does not check foreign key constraints out of the box [[1]](http://stackoverflow.com/questions/6745763/enable-integrity-checking-with-sqlite-in-django). - -SQLite is much easier to set up, but does not provide environment parity between development and production [[2]](http://12factor.net/dev-prod-parity). +* SQLite runs tests much faster +* SQLite does not check foreign key constraints out of the box [[1]](http://stackoverflow.com/questions/6745763/enable-integrity-checking-with-sqlite-in-django) [FIXED]. +* SQLite is easier to set up. +* SQLite does not provide environment parity between development and production [[2]](http://12factor.net/dev-prod-parity). diff --git a/dusken/__init__.py b/dusken/__init__.py index e69de29..5acaba9 100644 --- a/dusken/__init__.py +++ b/dusken/__init__.py @@ -0,0 +1,9 @@ +# Enable Foreign Keys in SQLite +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)