HeadlinesBriefing favicon HeadlinesBriefing.com

SQLite's Defaults Need Improvement

Hacker News •
×

SQLite is a powerful, embedded database engine often used for local data storage. However, its default settings can lead to data inconsistencies. One major issue is that foreign key constraints are ignored by default. This means that references between tables can become invalid without raising an error, potentially leading to corrupted data and unexpected behavior, as demonstrated by a scenario where deleting a user unexpectedly reassigns their posts to a new user with the same ID. The solution is to enable foreign keys using `PRAGMA foreign_keys = ON;`.

Another significant problem is SQLite's flexible typing system, where columns can accept data types other than what they are declared as due to 'affinity.' For instance, an INTEGER column can store text representations of numbers, and even non-numeric text, leading to data corruption. The introduction of `strict` tables offers a solution by enforcing type errors on incorrect insertions. However, there's no global pragma to enforce strictness across all tables, requiring manual application to each table. While SQLite's authors favor flexible typing, explicit declaration using `ANY` or `strict` tables provides better data integrity.