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:
Simon Willison 2019-05-28 21:54:43 -07:00
commit 50e2f94b58
6 changed files with 118 additions and 6 deletions

View file

@ -169,10 +169,12 @@ def optimize(path, no_vacuum):
),
required=False,
)
def add_column(path, table, col_name, col_type):
@click.option("--fk", type=str, required=False)
@click.option("--fk-col", type=str, required=False)
def add_column(path, table, col_name, col_type, fk, fk_col):
"Add a column to the specified table"
db = sqlite_utils.Database(path)
db[table].add_column(col_name, col_type)
db[table].add_column(col_name, col_type, fk=fk, fk_col=fk_col)
@cli.command(name="add-foreign-key")