question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foreign key enforcement in SQLite

See original GitHub issue

Foreign keys are disabled by default in SQLite3. It would be nice to have an optional enable_foreign_keys boolean argument to the SqliteDatabase constructor. It would have two effects. Suppose db = SqliteDatabase(filename, enable_foreign_keys=True). Then

  1. db.foreign_keys is True; and
  2. db.create_tables would issue
PRAGMA foreign_keys = ON;

Issuing that PRAGMA in the middle of a multi-statement transaction has no effect and swallows the error. It is only possible to turn on foreign_keys in SQLite versions greater than 3.6.19 when SQLite is compiled with SQLITE_OMIT_TRIGGER and SQLITE_OMIT_FOREIGN_KEY turned off. The versions of SQLite compiled into Python versions 2.7.9 and 3.4.3 on Windows appear to support foreign_keys.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
coleifercommented, Dec 11, 2016

SqliteDatabase can now be instantiated with pragmas, so:

db = SqliteDatabase('filename.db', pragmas=(('foreign_keys', 'on'),))
0reactions
coleifercommented, Dec 11, 2016

Specifying AUTOINCREMENT on id fields seems like a good idea.

This has performance implications. Hence, we use the default SQLite behavior but allow you to use auto-increment if you really want that. http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#PrimaryKeyAutoIncrementField

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite Foreign Key Support
SQL foreign key constraints are used to enforce "exists" relationships between tables. For example, consider a database schema created using the ...
Read more >
SQLite Foreign Key: Enforce Relationships Between Tables
This tutorial shows you how to use the SQLite foreign key constraint to enforce the relationships between correlated tables.
Read more >
sqlite - Foreign key isn't being enforced - Stack Overflow
Assuming the library is compiled with foreign key constraints enabled, it must still be enabled by the application at runtime, using the PRAGMA ......
Read more >
Primary Key and Foreign Key in SQLite with Examples - Guru99
The SQLite foreign key is a constraint that verifies the existence of value present in one table to another table that has a...
Read more >
How to Enable Foreign Key Support in SQLite - Database.Guide
Enable Foreign Keys at Runtime ... Even when the library has been compiled with foreign key constraints enabled, you still need to enable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found