mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Ability to add a column that is a foreign key reference
Python API:
db["dogs"].add_column("species_id", fk="species")
# or
db["dogs"].add_column("species_id", fk="species", fk_col="ref")
CLI:
$ sqlite-utils add-column mydb.db dogs species_id --fk species
# or
$ sqlite-utils add-column mydb.db dogs species_id --fk species --fk-col ref
Closes #16
This commit is contained in:
parent
00c5a49a87
commit
50e2f94b58
6 changed files with 118 additions and 6 deletions
|
|
@ -266,6 +266,20 @@ If you pass a Python type, it will be mapped to SQLite types as shown here::
|
|||
np.float32: "FLOAT"
|
||||
np.float64: "FLOAT"
|
||||
|
||||
You can also add a column that is a foreign key reference to another table using the ``fk`` parameter:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
db["dogs"].add_column("species_id", fk="species")
|
||||
|
||||
This will automatically detect the name of the primary key on the species table and use that (and its type) for the new column.
|
||||
|
||||
You can explicitly specify the column you wish to reference using ``fk_col``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
db["dogs"].add_column("species_id", fk="species", fk_col="ref")
|
||||
|
||||
.. _python_api_add_column_alter:
|
||||
|
||||
Adding columns automatically on insert/update
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue