diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2023-01-09 19:36:36 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2023-01-09 19:36:36 +0000 |
| commit | 718f865a7840da0f9a819086acbeba3ccb80408b (patch) | |
| tree | 9db43f87d98221f3451724bc22df3f168870561f /lib/sqlalchemy/orm | |
| parent | f7580186cd50ab9174083f2df7dbf26fc54b9f40 (diff) | |
| parent | eb9d00c4b4f4f15e871aa9ea88d41023054c6e97 (diff) | |
| download | sqlalchemy-718f865a7840da0f9a819086acbeba3ccb80408b.tar.gz | |
Merge "accept TableClause through mapped selectable chain" into main
Diffstat (limited to 'lib/sqlalchemy/orm')
| -rw-r--r-- | lib/sqlalchemy/orm/mapper.py | 26 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/session.py | 7 |
2 files changed, 14 insertions, 19 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 522028274..20ad635b0 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -78,6 +78,7 @@ from ..sql import coercions from ..sql import expression from ..sql import operators from ..sql import roles +from ..sql import TableClause from ..sql import util as sql_util from ..sql import visitors from ..sql.cache_key import MemoizedHasCacheKey @@ -892,7 +893,7 @@ class Mapper( _dependency_processors: List[DependencyProcessor] _memoized_values: Dict[Any, Callable[[], Any]] _inheriting_mappers: util.WeakSequence[Mapper[Any]] - _all_tables: Set[Table] + _all_tables: Set[TableClause] _polymorphic_attr_key: Optional[str] _pks_by_table: Dict[FromClause, OrderedSet[ColumnClause[Any]]] @@ -908,9 +909,10 @@ class Mapper( Callable[[Mapper[_O], InstanceState[_O], _InstanceDict], None] ] - tables: Sequence[Table] - """A sequence containing the collection of :class:`_schema.Table` objects - which this :class:`_orm.Mapper` is aware of. + tables: Sequence[TableClause] + """A sequence containing the collection of :class:`_schema.Table` + or :class:`_schema.TableClause` objects which this :class:`_orm.Mapper` + is aware of. If the mapper is mapped to a :class:`_expression.Join`, or an :class:`_expression.Alias` @@ -1534,17 +1536,9 @@ class Mapper( self.__dict__.pop("_configure_failed", None) def _configure_pks(self) -> None: - self.tables = cast( - "List[Table]", sql_util.find_tables(self.persist_selectable) - ) - for t in self.tables: - if not isinstance(t, Table): - raise sa_exc.ArgumentError( - f"ORM mappings can only be made against schema-level " - f"Table objects, not TableClause; got " - f"tableclause {t.name !r}" - ) - self._all_tables.update(t for t in self.tables if isinstance(t, Table)) + self.tables = sql_util.find_tables(self.persist_selectable) + + self._all_tables.update(t for t in self.tables) self._pks_by_table = {} self._cols_by_table = {} @@ -3802,7 +3796,7 @@ class Mapper( @HasMemoized.memoized_attribute def _sorted_tables(self): - table_to_mapper: Dict[Table, Mapper[Any]] = {} + table_to_mapper: Dict[TableClause, Mapper[Any]] = {} for mapper in self.base_mapper.self_and_descendants: for t in mapper.tables: diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 0298b17a7..5bcb22a08 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -80,6 +80,7 @@ from ..sql import coercions from ..sql import dml from ..sql import roles from ..sql import Select +from ..sql import TableClause from ..sql import visitors from ..sql.base import CompileState from ..sql.schema import Table @@ -152,7 +153,7 @@ _PKIdentityArgument = Union[Any, Tuple[Any, ...]] _BindArguments = Dict[str, Any] _EntityBindKey = Union[Type[_O], "Mapper[_O]"] -_SessionBindKey = Union[Type[Any], "Mapper[Any]", "Table", str] +_SessionBindKey = Union[Type[Any], "Mapper[Any]", "TableClause", str] _SessionBind = Union["Engine", "Connection"] JoinTransactionMode = Literal[ @@ -2439,7 +2440,7 @@ class Session(_SessionClassMethods, EventTarget): if TYPE_CHECKING: assert isinstance(insp, Inspectable) - if isinstance(insp, Table): + if isinstance(insp, TableClause): self.__binds[insp] = bind elif insp_is_mapper(insp): self.__binds[insp.class_] = bind @@ -2480,7 +2481,7 @@ class Session(_SessionClassMethods, EventTarget): """ self._add_bind(mapper, bind) - def bind_table(self, table: Table, bind: _SessionBind) -> None: + def bind_table(self, table: TableClause, bind: _SessionBind) -> None: """Associate a :class:`_schema.Table` with a "bind", e.g. an :class:`_engine.Engine` or :class:`_engine.Connection`. |
