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'])
| 10 | |
| 11 | |
| 12 | def 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 | |
| 33 | def get_full_name_for_sql(model: Union[Table, Enum]) -> str: |