mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 04:44:26 +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
|
|
@ -195,6 +195,25 @@ def test_add_foreign_key_error_if_already_exists(fresh_db):
|
|||
assert "Foreign key already exists for author_id => authors.id" == ex.value.args[0]
|
||||
|
||||
|
||||
def test_add_column_foreign_key(fresh_db):
|
||||
fresh_db.create_table("dogs", {"name": str})
|
||||
fresh_db.create_table("breeds", {"name": str})
|
||||
fresh_db["dogs"].add_column("breed_id", fk="breeds")
|
||||
assert (
|
||||
"CREATE TABLE [dogs] ( [name] TEXT , [breed_id] INTEGER, FOREIGN KEY(breed_id) REFERENCES breeds(rowid) )"
|
||||
== collapse_whitespace(fresh_db["dogs"].schema)
|
||||
)
|
||||
# And again with an explicit primary key column
|
||||
fresh_db.create_table("subbreeds", {"name": str, "primkey": str}, pk="primkey")
|
||||
fresh_db["dogs"].add_column("subbreed_id", fk="subbreeds")
|
||||
assert (
|
||||
"CREATE TABLE [dogs] ( [name] TEXT , [breed_id] INTEGER, [subbreed_id] TEXT, "
|
||||
"FOREIGN KEY(breed_id) REFERENCES breeds(rowid), "
|
||||
"FOREIGN KEY(subbreed_id) REFERENCES subbreeds(primkey) )"
|
||||
== collapse_whitespace(fresh_db["dogs"].schema)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"extra_data,expected_new_columns",
|
||||
[
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue