db.m2m_table_candidates(table, other_table)

This commit is contained in:
Simon Willison 2019-08-03 21:07:06 +03:00
commit b9256413d2
2 changed files with 33 additions and 0 deletions

View file

@ -297,6 +297,17 @@ class Database:
)
)
def m2m_table_candidates(self, table, other_table):
"Returns potential m2m tables for arguments, based on FKs"
candidates = []
tables = {table, other_table}
for table in self.tables:
# Does it have foreign keys to both table and other_table?
has_fks_to = {fk.other_table for fk in table.foreign_keys}
if has_fks_to.issuperset(tables):
candidates.append(table.name)
return candidates
def add_foreign_keys(self, foreign_keys):
# foreign_keys is a list of explicit 4-tuples
assert all(