You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
.add_foreign_keys() uses .transform() instead of PRAGMA writable_schema
Closes#577
This should solve all sorts of problems seen by users of platforms that throw errors on writable_schema.
Also added `add_foreign_keys=` and `foreign_keys=` parameters to `table.transform()`.
The SQLite ``ALTER TABLE`` statement doesn't have the ability to add foreign key references to an existing column.
1283
1283
1284
-
It's possible to add these references through very careful manipulation of SQLite's ``sqlite_master`` table, using ``PRAGMA writable_schema``.
1285
-
1286
-
``sqlite-utils`` can do this for you, though there is a significant risk of data corruption if something goes wrong so it is advisable to create a fresh copy of your database file before attempting this.
1284
+
The ``add_foreign_key()`` method here is a convenient wrapper around :ref:`table.transform() <python_api_transform>`.
1287
1285
1288
1286
Here's an example of this mechanism in action:
1289
1287
@@ -1317,11 +1315,7 @@ To ignore the case where the key already exists, use ``ignore=True``:
1317
1315
Adding multiple foreign key constraints at once
1318
1316
-----------------------------------------------
1319
1317
1320
-
The final step in adding a new foreign key to a SQLite database is to run ``VACUUM``, to ensure the new foreign key is available in future introspection queries.
1321
-
1322
-
``VACUUM`` against a large (multi-GB) database can take several minutes or longer. If you are adding multiple foreign keys using ``table.add_foreign_key(...)`` these can quickly add up.
1323
-
1324
-
Instead, you can use ``db.add_foreign_keys(...)`` to add multiple foreign keys within a single transaction. This method takes a list of four-tuples, each one specifying a ``table``, ``column``, ``other_table`` and ``other_column``.
1318
+
You can use ``db.add_foreign_keys(...)`` to add multiple foreign keys in one go. This method takes a list of four-tuples, each one specifying a ``table``, ``column``, ``other_table`` and ``other_column``.
1325
1319
1326
1320
Here's an example adding two foreign keys at once:
1327
1321
@@ -1388,6 +1382,8 @@ To keep the original table around instead of dropping it, pass the ``keep_table=
You can add one or more foreign key constraints to a table using the ``add_foreign_keys=`` parameter:
1490
+
1491
+
.. code-block:: python
1492
+
1493
+
db["places"].transform(
1494
+
add_foreign_keys=(
1495
+
("country", "country", "id"),
1496
+
("continent", "continent", "id")
1497
+
)
1498
+
)
1499
+
1500
+
This accepts the same arguments described in :ref:`specifying foreign keys <python_api_foreign_keys>`- so you can specify them as a full tuple of ``(column, other_table, other_column)``, or you can take a shortcut andpass just the name of the column, provided the table can be automatically derived from the column name:
The ``foreign_keys=`` parameter is similar to to ``add_foreign_keys=`` but can be be used to replace all foreign key constraints on a table, dropping any that are not explicitly mentioned:
0 commit comments