MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _sorted_tables

Method _sorted_tables

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

Source from the content-addressed store, hash-verified

4033
4034 @HasMemoized.memoized_attribute
4035 def _sorted_tables(self):
4036 table_to_mapper: Dict[TableClause, Mapper[Any]] = {}
4037
4038 for mapper in self.base_mapper.self_and_descendants:
4039 for t in mapper.tables:
4040 table_to_mapper.setdefault(t, mapper)
4041
4042 extra_dependencies = []
4043 for table, mapper in table_to_mapper.items():
4044 super_ = mapper.inherits
4045 if super_:
4046 extra_dependencies.extend(
4047 [(super_table, table) for super_table in super_.tables]
4048 )
4049
4050 def skip(fk):
4051 # attempt to skip dependencies that are not
4052 # significant to the inheritance chain
4053 # for two tables that are related by inheritance.
4054 # while that dependency may be important, it's technically
4055 # not what we mean to sort on here.
4056 parent = table_to_mapper.get(fk.parent.table)
4057 dep = table_to_mapper.get(fk.column.table)
4058 if (
4059 parent is not None
4060 and dep is not None
4061 and dep is not parent
4062 and dep.inherit_condition is not None
4063 ):
4064 cols = set(sql_util._find_columns(dep.inherit_condition))
4065 if parent.inherit_condition is not None:
4066 cols = cols.union(
4067 sql_util._find_columns(parent.inherit_condition)
4068 )
4069 return fk.parent not in cols and fk.column not in cols
4070 else:
4071 return fk.parent not in cols
4072 return False
4073
4074 sorted_ = sql_util.sort_tables(
4075 table_to_mapper,
4076 skip_fn=skip,
4077 extra_dependencies=extra_dependencies,
4078 )
4079
4080 ret = util.OrderedDict()
4081 for t in sorted_:
4082 ret[t] = table_to_mapper[t]
4083 return ret
4084
4085 def _memo(self, key: Any, callable_: Callable[[], _T]) -> _T:
4086 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