diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-05-10 22:03:18 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2023-05-10 22:05:45 -0400 |
| commit | 73a273c90cda2369ec071435edd9c6dc5c1d31c4 (patch) | |
| tree | 27a79e1c0800fffb6a1bce70830b779c0de16251 /lib/sqlalchemy | |
| parent | e00591ec27f63d9cc851bbb3cf4824bd5644a8b8 (diff) | |
| download | sqlalchemy-73a273c90cda2369ec071435edd9c6dc5c1d31c4.tar.gz | |
return empty MappedColumn() at early pep-593 step
Fixed issue in new ORM Annotated Declarative where using a
:class:`_schema.ForeignKey` (or other column-level constraint) inside of
:func:`_orm.mapped_column` which is then copied out to models via pep-593
``Annotated`` would apply duplicates of each constraint to the
:class:`_schema.Column` as produced in the target :class:`_schema.Table`,
leading to incorrect CREATE TABLE DDL as well as migration directives under
Alembic.
Fixes: #9766
Change-Id: I8a3b2716bf393d1d2b5894f9f72b45fa59df1e08
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/properties.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 916b9d901..dd53a9536 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -592,7 +592,6 @@ class MappedColumn( None, SchemaConst.NULL_UNSPECIFIED, ) - util.set_creation_order(self) def _copy(self, **kw: Any) -> Self: @@ -649,7 +648,9 @@ class MappedColumn( return op(col._bind_param(op, other), col, **kwargs) # type: ignore[return-value] # noqa: E501 def found_in_pep593_annotated(self) -> Any: - return self._copy() + # return a blank mapped_column(). This mapped_column()'s + # Column will be merged into it in _init_column_for_annotation(). + return MappedColumn() def declarative_scan( self, @@ -751,13 +752,15 @@ class MappedColumn( if is_pep593(our_type): our_type_is_pep593 = True + pep_593_components = typing_get_args(our_type) raw_pep_593_type = pep_593_components[0] if is_optional_union(raw_pep_593_type): + raw_pep_593_type = de_optionalize_union_types(raw_pep_593_type) + nullable = True if not self._has_nullable: self.column.nullable = nullable - raw_pep_593_type = de_optionalize_union_types(raw_pep_593_type) for elem in pep_593_components[1:]: if isinstance(elem, MappedColumn): use_args_from = elem @@ -772,6 +775,7 @@ class MappedColumn( and use_args_from.column.default is not None ): self.column.default = None + use_args_from.column._merge(self.column) sqltype = self.column.type |
