mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
sqlite-utils add-foreign-keys command, closes #157
This commit is contained in:
parent
7805d53bcf
commit
3cc1944e53
3 changed files with 81 additions and 0 deletions
|
|
@ -323,6 +323,36 @@ def add_foreign_key(path, table, column, other_table, other_column):
|
|||
raise click.ClickException(e)
|
||||
|
||||
|
||||
@cli.command(name="add-foreign-keys")
|
||||
@click.argument(
|
||||
"path",
|
||||
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
|
||||
required=True,
|
||||
)
|
||||
@click.argument("foreign_key", nargs=-1)
|
||||
def add_foreign_keys(path, foreign_key):
|
||||
"""
|
||||
Add multiple new foreign key constraints to a database. Example usage:
|
||||
|
||||
\b
|
||||
sqlite-utils add-foreign-keys my.db \\
|
||||
books author_id authors id \\
|
||||
authors country_id countries id
|
||||
"""
|
||||
db = sqlite_utils.Database(path)
|
||||
if len(foreign_key) % 4 != 0:
|
||||
raise click.ClickException(
|
||||
"Each foreign key requires four values: table, column, other_table, other_column"
|
||||
)
|
||||
tuples = []
|
||||
for i in range(len(foreign_key) // 4):
|
||||
tuples.append(tuple(foreign_key[i * 4 : (i * 4) + 4]))
|
||||
try:
|
||||
db.add_foreign_keys(tuples)
|
||||
except AlterError as e:
|
||||
raise click.ClickException(e)
|
||||
|
||||
|
||||
@cli.command(name="index-foreign-keys")
|
||||
@click.argument(
|
||||
"path",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue