MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _sorted_tables

Method _sorted_tables

lib/sqlalchemy/orm/mapper.py:4007–4055  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4005
4006 @HasMemoized.memoized_attribute
4007 def _sorted_tables(self):
4008 table_to_mapper: Dict[TableClause, Mapper[Any]] = {}
4009
4010 for mapper in self.base_mapper.self_and_descendants:
4011 for t in mapper.tables:
4012 table_to_mapper.setdefault(t, mapper)
4013
4014 extra_dependencies = []
4015 for table, mapper in table_to_mapper.items():
4016 super_ = mapper.inherits
4017 if super_:
4018 extra_dependencies.extend(
4019 [(super_table, table) for super_table in super_.tables]
4020 )
4021
4022 def skip(fk):
4023 # attempt to skip dependencies that are not
4024 # significant to the inheritance chain
4025 # for two tables that are related by inheritance.
4026 # while that dependency may be important, it's technically
4027 # not what we mean to sort on here.
4028 parent = table_to_mapper.get(fk.parent.table)
4029 dep = table_to_mapper.get(fk.column.table)
4030 if (
4031 parent is not None
4032 and dep is not None
4033 and dep is not parent
4034 and dep.inherit_condition is not None
4035 ):
4036 cols = set(sql_util._find_columns(dep.inherit_condition))
4037 if parent.inherit_condition is not None:
4038 cols = cols.union(
4039 sql_util._find_columns(parent.inherit_condition)
4040 )
4041 return fk.parent not in cols and fk.column not in cols
4042 else:
4043 return fk.parent not in cols
4044 return False
4045
4046 sorted_ = sql_util.sort_tables(
4047 table_to_mapper,
4048 skip_fn=skip,
4049 extra_dependencies=extra_dependencies,
4050 )
4051
4052 ret = util.OrderedDict()
4053 for t in sorted_:
4054 ret[t] = table_to_mapper[t]
4055 return ret
4056
4057 def _memo(self, key: Any, callable_: Callable[[], _T]) -> _T:
4058 if key in self._memoized_values:

Callers

nothing calls this directly

Calls 3

setdefaultMethod · 0.45
itemsMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected