diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-04-10 12:56:47 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-04-12 15:11:03 -0400 |
commit | 9f43b10e9014e694cb89fe2899dc52f602bf2197 (patch) | |
tree | 03c54cbc80bd98e9d358b6a21a029b8524142696 /lib/sqlalchemy/orm/properties.py | |
parent | 6e5ed192c6435ec107eae524bb2c6959c38bc654 (diff) | |
download | sqlalchemy-9f43b10e9014e694cb89fe2899dc52f602bf2197.tar.gz |
establish column_property and query_expression as readonly from a dc perspective
Fixed bug in ORM Declarative Dataclasses where the
:func:`_orm.queryable_attribute` and :func:`_orm.column_property`
constructs, which are documented as read-only constructs in the context of
a Declarative mapping, could not be used with a
:class:`_orm.MappedAsDataclass` class without adding ``init=False``, which
in the case of :func:`_orm.queryable_attribute` was not possible as no
``init`` parameter was included. These constructs have been modified from a
dataclass perspective to be assumed to be "read only", setting
``init=False`` by default and no longer including them in the pep-681
constructor. The dataclass parameters for :func:`_orm.column_property`
``init``, ``default``, ``default_factory``, ``kw_only`` are now deprecated;
these fields don't apply to :func:`_orm.column_property` as used in a
Declarative dataclasses configuration where the construct would be
read-only. Also added read-specific parameter
:paramref:`_orm.queryable_attribute.compare` to
:func:`_orm.queryable_attribute`; :paramref:`_orm.queryable_attribute.repr`
was already present.
Added missing :paramref:`_orm.mapped_column.active_history` parameter
to :func:`_orm.mapped_column` construct.
Fixes: #9628
Change-Id: I2ab44d6b763b20410bd1ebb5ac949a6d223f1ce2
Diffstat (limited to 'lib/sqlalchemy/orm/properties.py')
-rw-r--r-- | lib/sqlalchemy/orm/properties.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 2f7b85d88..f00775874 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -151,8 +151,12 @@ class ColumnProperty( info: Optional[_InfoType] = None, doc: Optional[str] = None, _instrument: bool = True, + _assume_readonly_dc_attributes: bool = False, ): - super().__init__(attribute_options=attribute_options) + super().__init__( + attribute_options=attribute_options, + _assume_readonly_dc_attributes=_assume_readonly_dc_attributes, + ) columns = (column,) + additional_columns self.columns = [ coercions.expect(roles.LabeledColumnExprRole, c) for c in columns @@ -532,6 +536,7 @@ class MappedColumn( "deferred", "deferred_group", "deferred_raiseload", + "active_history", "_attribute_options", "_has_dataclass_arguments", "_use_existing_column", @@ -579,7 +584,7 @@ class MappedColumn( self.deferred = bool( self.deferred_group or self.deferred_raiseload ) - + self.active_history = kw.pop("active_history", False) self._sort_order = kw.pop("sort_order", 0) self.column = cast("Column[_T]", Column(*arg, **kw)) self.foreign_keys = self.column.foreign_keys @@ -597,6 +602,7 @@ class MappedColumn( new.deferred_group = self.deferred_group new.deferred_raiseload = self.deferred_raiseload new.foreign_keys = new.column.foreign_keys + new.active_history = self.active_history new._has_nullable = self._has_nullable new._attribute_options = self._attribute_options new._has_insert_default = self._has_insert_default @@ -612,13 +618,14 @@ class MappedColumn( @property def mapper_property_to_assign(self) -> Optional[MapperProperty[_T]]: - if self.deferred: + if self.deferred or self.active_history: return ColumnProperty( self.column, - deferred=True, + deferred=self.deferred, group=self.deferred_group, raiseload=self.deferred_raiseload, attribute_options=self._attribute_options, + active_history=self.active_history, ) else: return None |