Hacker News new | ask | show | jobs
by simonw 1466 days ago
You don't need to create a new table to add foreign keys - you can do it by setting PRAGMA writable_schema = 1, then updating the table definition directly in the sqlite_master table to include the foreign key constraints.

This sounds terrifyingly dangerous, so you should absolutely take a backup first - but I've found it to work just fine in practice.

Here's where I implement that in my sqlite-utils Python library and CLI tool:

https://github.com/simonw/sqlite-utils/blob/2d84577202e22767...