def find_models(module: ModuleType) -> List[Type[Model]]:
    '\n    在迁移脚本中查找所有模型。\n\n    :param module:\n    :return:\n    '
    models: List[Type[Model]] = []
    tables = extract_modified_tables(module)
    queue = list(module.__dict__.values())
    while queue:
        obj = queue.pop()
        if hasattr(obj, '__tablename__'):
            tables.add(obj.__tablename__)
        elif isinstance(obj, list):
            queue.extend(obj)
        elif isinstance(obj, dict):
            queue.extend(obj.values())
    for obj in Model._decl_class_registry.values():
        if (hasattr(obj, '__table__') and (obj.__table__.fullname in tables)):
            models.append(obj)
    sorter = TopologicalSorter()
    for model in models:
        inspector = inspect(model)
        dependent_tables: List[str] = []
        for column in inspector.columns.values():
            for foreign_key in column.foreign_keys:
                dependent_tables.append(foreign_key.target_fullname.split('.')[0])
        sorter.add(model.__tablename__, *dependent_tables)
    order = list(sorter.static_order())
    models.sort(key=(lambda model: order.index(model.__tablename__)))
    return models
在迁移脚本中查找所有模型。

:param module:
:return:
def find_models(module: ModuleType) -> List[Type[Model]]:
    '\n    在迁移脚本中查找所有模型。\n\n    :param module:\n    :return:\n    '
    models: List[Type[Model]] = []
    tables = extract_modified_tables(module)
    queue = list(module.__dict__.values())
    while queue:
        obj = queue.pop()
        if hasattr(obj, '__tablename__'):
            tables.add(obj.__tablename__)
        elif isinstance(obj, list):
            queue.extend(obj)
        elif isinstance(obj, dict):
            queue.extend(obj.values())
    for obj in Model._decl_class_registry.values():
        if (hasattr(obj, '__table__') and (obj.__table__.fullname in tables)):
            models.append(obj)
    sorter = TopologicalSorter()
    for model in models:
        inspector = inspect(model)
        dependent_tables: List[str] = []
        for column in inspector.columns.values():
            for foreign_key in column.foreign_keys:
                dependent_tables.append(foreign_key.target_fullname.split('.')[0])
        sorter.add(model.__tablename__, *dependent_tables)
    order = list(sorter.static_order())
    models.sort(key=(lambda model: order.index(model.__tablename__)))
    return models