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:
Simon Willison 2020-09-20 15:17:25 -07:00
commit e23eedb4ce
6 changed files with 48 additions and 9 deletions

View file

@ -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,