Include foreign key info in inspect() output

Refs #85
This commit is contained in:
Simon Willison 2017-11-15 17:34:32 -08:00
commit a4af532a31
2 changed files with 43 additions and 7 deletions

View file

@ -197,3 +197,32 @@ def temporary_docker_directory(files, name, metadata, extra_options, extra_metad
finally:
tmp.cleanup()
os.chdir(saved_cwd)
def get_all_foreign_keys(conn):
tables = [r[0] for r in conn.execute('select name from sqlite_master where type="table"')]
table_to_foreign_keys = {}
for table in tables:
table_to_foreign_keys[table] = {
'incoming': [],
'outgoing': [],
}
for table in tables:
infos = conn.execute(
'PRAGMA foreign_key_list([{}])'.format(table)
).fetchmany()
for info in infos:
if info is not None:
id, seq, table_name, from_, to_, on_update, on_delete, match = info
table_to_foreign_keys[table_name]['incoming'].append({
'other_table': table,
'column': to_,
'other_column': from_
})
table_to_foreign_keys[table]['outgoing'].append({
'other_table': table_name,
'column': from_,
'other_column': to_
})
return table_to_foreign_keys