mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 17:34:32 +02:00
ignore=True argument for add_foreign_key, closes #112
Also --ignore for add-foreign-key command Plus table.add_foreign_key(...) now returns self, allowing more chaining
This commit is contained in:
parent
ecb50c8f76
commit
e23eedb4ce
6 changed files with 48 additions and 9 deletions
|
|
@ -781,7 +781,9 @@ class Table(Queryable):
|
|||
else:
|
||||
return pks[0].name
|
||||
|
||||
def add_foreign_key(self, column, other_table=None, other_column=None):
|
||||
def add_foreign_key(
|
||||
self, column, other_table=None, other_column=None, ignore=False
|
||||
):
|
||||
# Ensure column exists
|
||||
if column not in self.columns_dict:
|
||||
raise AlterError("No such column: {}".format(column))
|
||||
|
|
@ -806,12 +808,16 @@ class Table(Queryable):
|
|||
and fk.other_table == other_table
|
||||
and fk.other_column == other_column
|
||||
):
|
||||
raise AlterError(
|
||||
"Foreign key already exists for {} => {}.{}".format(
|
||||
column, other_table, other_column
|
||||
if ignore:
|
||||
return self
|
||||
else:
|
||||
raise AlterError(
|
||||
"Foreign key already exists for {} => {}.{}".format(
|
||||
column, other_table, other_column
|
||||
)
|
||||
)
|
||||
)
|
||||
self.db.add_foreign_keys([(self.name, column, other_table, other_column)])
|
||||
return self
|
||||
|
||||
def enable_fts(
|
||||
self,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue