MCPcopy Index your code
hub / github.com/Vanderhoof/PyDBML / reorder_tables_for_sql

Function reorder_tables_for_sql

pydbml/renderer/sql/default/utils.py:12–30  ·  view source on GitHub ↗

Attempt to reorder the tables, so that they are defined in SQL before they are referenced by inline foreign keys. Won't aid the rare cases of cross-references and many-to-many relations.

(tables: List['Table'], refs: List['Reference'])

Source from the content-addressed store, hash-verified

10
11
12def reorder_tables_for_sql(tables: List['Table'], refs: List['Reference']) -> List['Table']:
13 """
14 Attempt to reorder the tables, so that they are defined in SQL before they are referenced by
15 inline foreign keys.
16
17 Won't aid the rare cases of cross-references and many-to-many relations.
18 """
19
20 references: Dict[str, int] = {}
21 for ref in refs:
22 if ref.inline:
23 if ref.type == MANY_TO_ONE and ref.table1 is not None:
24 table_name = ref.table1.name
25 elif ref.type == ONE_TO_MANY and ref.table2 is not None:
26 table_name = ref.table2.name
27 else: # pragma: no cover
28 continue
29 references[table_name] = references.get(table_name, 0) + 1
30 return sorted(tables, key=lambda t: references.get(t.name, 0), reverse=True)
31
32
33def get_full_name_for_sql(model: Union[Table, Enum]) -> str:

Callers 2

test_reorder_tablesFunction · 0.90
render_dbMethod · 0.90

Calls 1

getMethod · 0.80

Tested by 1

test_reorder_tablesFunction · 0.72