summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/mapper.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-04-29 23:26:36 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-05-18 17:46:10 -0400
commitf07e050c9ce4afdeb9c0c136dbcc547f7e5ac7b8 (patch)
tree1b3cd7409ae2eddef635960126551d74f469acc1 /lib/sqlalchemy/orm/mapper.py
parent614dfb5f5b5a2427d5d6ce0bc5f34bf0581bf698 (diff)
downloadsqlalchemy-f07e050c9ce4afdeb9c0c136dbcc547f7e5ac7b8.tar.gz
Implement new ClauseElement role and coercion system
A major refactoring of all the functions handle all detection of Core argument types as well as perform coercions into a new class hierarchy based on "roles", each of which identify a syntactical location within a SQL statement. In contrast to the ClauseElement hierarchy that identifies "what" each object is syntactically, the SQLRole hierarchy identifies the "where does it go" of each object syntactically. From this we define a consistent type checking and coercion system that establishes well defined behviors. This is a breakout of the patch that is reorganizing select() constructs to no longer be in the FromClause hierarchy. Also includes a rename of as_scalar() into scalar_subquery(); deprecates automatic coercion to scalar_subquery(). Partially-fixes: #4617 Change-Id: I26f1e78898693c6b99ef7ea2f4e7dfd0e8e1a1bd
Diffstat (limited to 'lib/sqlalchemy/orm/mapper.py')
-rw-r--r--lib/sqlalchemy/orm/mapper.py29
1 files changed, 14 insertions, 15 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 6eadffb16..ccf05a783 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -45,8 +45,10 @@ from .. import log
from .. import schema
from .. import sql
from .. import util
+from ..sql import coercions
from ..sql import expression
from ..sql import operators
+from ..sql import roles
from ..sql import util as sql_util
from ..sql import visitors
@@ -644,8 +646,14 @@ class Mapper(InspectionAttr):
self.batch = batch
self.eager_defaults = eager_defaults
self.column_prefix = column_prefix
- self.polymorphic_on = expression._clause_element_as_expr(
- polymorphic_on
+ self.polymorphic_on = (
+ coercions.expect(
+ roles.ColumnArgumentOrKeyRole,
+ polymorphic_on,
+ argname="polymorphic_on",
+ )
+ if polymorphic_on is not None
+ else None
)
self._dependency_processors = []
self.validators = util.immutabledict()
@@ -1548,14 +1556,6 @@ class Mapper(InspectionAttr):
"can be passed for polymorphic_on"
)
prop = self.polymorphic_on
- elif not expression._is_column(self.polymorphic_on):
- # polymorphic_on is not a Column and not a ColumnProperty;
- # not supported right now.
- raise sa_exc.ArgumentError(
- "Only direct column-mapped "
- "property or SQL expression "
- "can be passed for polymorphic_on"
- )
else:
# polymorphic_on is a Column or SQL expression and
# doesn't appear to be mapped. this means it can be 1.
@@ -1851,11 +1851,7 @@ class Mapper(InspectionAttr):
# generate a properties.ColumnProperty
columns = util.to_list(prop)
column = columns[0]
- if not expression._is_column(column):
- raise sa_exc.ArgumentError(
- "%s=%r is not an instance of MapperProperty or Column"
- % (key, prop)
- )
+ assert isinstance(column, expression.ColumnElement)
prop = self._props.get(key, None)
@@ -2260,6 +2256,9 @@ class Mapper(InspectionAttr):
for table, columns in self._cols_by_table.items()
)
+ def __clause_element__(self):
+ return self.selectable
+
@property
def selectable(self):
"""The :func:`.select` construct this :class:`.Mapper` selects from