Skip to main content

sqlite-utils-fast-fks

PyPI Changelog Tests License

Fast foreign key addition for sqlite-utils.

Background

SQLite does not yet have a built-in method for adding foreign key constraints to an existing table.

There are two workarounds for this limitation:

  1. You can create brand new table with the new foreign keys, copy the data across, then drop the old table and rename the new one. This method is implemented by the sqlite-utils table.transform() method.
  2. You can set PRAGMA writable_schema = 1, then directly modify the schema stored in the sqlite_master table for that table. Then increment the schema_version, set writable_schema = 0 again and run a vacuum against the database.

For tables with large numbers of rows that second option is a lot faster, as you don't need to create an entirely new copy of all of the data.

Prior to version 3.35 sqlite-utils implemented the latter pattern as part of its table.add_foreign_key() and db.add_foreign_keys() methods.

It turned out these caused table sqlite_master may not be modified errors on some Python installations, primarily on macOS where the ability to modify the sqlite_master table is sometimes disabled by default.

This plugin brings the same functionality back again. You can use this if you want fast foreign key addition and you know that your platform does not suffer from the table sqlite_master may not be modified error.

Installation

Install this plugin in the same environment as sqlite-utils.

sqlite-utils install sqlite-utils-fast-fks

Or install using pip:

pip install sqlite-utils-fast-fks

Python library

To add foreign keys in Python code, use the add_foreign_keys(db, foreign_keys) function. Here's an example:

from sqlite_utils_fast_fks import add_foreign_keys
from sqlite_utils import Database

db = Database("my_database.db")
db["country"].insert_all([{"id": 1, "name": "United Kingdom"}])
db["continent"].insert_all([{"id": 1, "name": "Europe"}])
db["places"].insert(
    {
        "id": 1,
        "name": "London",
        "country_id": 1,
        "continent_id": 1,
    }
)

# Now modify that places table to have two foreign keys:
add_foreign_keys(
    db,
    [
        ("places", "country_id", "country", "id"),
        ("places", "continent_id", "continent", "id"),
    ],
)

The foreign_keys argument is a list of tuples, each containing four values:

  • The table to add the foreign key to
  • The column in that table that will be a foreign key
  • The other table that the column should reference
  • The column in that other table that should be referenced

Command-line tool

When installed as a sqlite-utils plugin, this library adds a new sqlite-utils fast-fks command. It can be used like this:

sqlite-utils fast-fks my_database.db places country_id country id

The command takes a path to a database, then the table name, the column name, the other table name and the other column name.

You can specify multiple foreign keys to add at once by repeating the last four arguments:

sqlite-utils fast-fks my_database.db \
    places country_id country id \
    places continent_id continent id

Development

To set up this plugin locally, first checkout the code. Then create a new virtual environment:

cd sqlite-utils-fast-fks
python3 -m venv venv
source venv/bin/activate

Now install the dependencies and test dependencies:

pip install -e '.[test]'

To run the tests:

pytest

Release files for sqlite-utils-fast-fks 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlite-utils-fast-fks 0.1
File Size Uploaded
sqlite-utils-fast-fks-0.1.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlite-utils-fast-fks 0.1
File Interpreter ABI Platform
sqlite_utils_fast_fks-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.9 kB

Release files / sqlite-utils-fast-fks-0.1.tar.gz

Download URL sqlite-utils-fast-fks-0.1.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8e3827ad1ab37f8d88b651729ba6661c6359eeabd03cdac547cdd4e8e7785e86
BLAKE2b-256 checksum
How to use checksums
2dd44e8fcc621d3373be387da948e57ff420e7a359aabdfbd547efbcdfd8d191
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / sqlite_utils_fast_fks-0.1-py3-none-any.whl

Download URL sqlite_utils_fast_fks-0.1-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
130b0582a5d8b9b3a4ac7f8d72d1c4efc4c8b99388e22cfceccf3e1574f7d706
BLAKE2b-256 checksum
How to use checksums
2b01a7ce32a7bc3540e6a9707a26426203ce79906f3e84db496f05e4f3d2430e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page