Archived
Enable Foreign Key support on sqlite
This commit is contained in:
1 parent
aca11a7381
commit
c8ff48a496
2 files changed
+13
-3
No files matched your search
@@ -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).
|
||||
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user