From 7842678484b9d00a64fab29cdc9e252754ac19ae Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 21 Aug 2022 11:58:20 -0400 Subject: Column._copy() duplicates "user defined" nullable state exactly To accommodate how mapped_column() works, after many attempts to get this working it became clear that _copy() should just transfer "nullable" state exactly as it was, including the state where .nullable was set but user_defined_nullable remains at not user set. additionally, added a similar step to _merge() that was needed to preserve the nullability behavior when Identity is present. server / client default objects are not copied within column._copy() and this should be fixed. Fixes: #8410 Change-Id: Ib09df52b71f3e58e67e9f19b893d40a6cc4eec5c --- lib/sqlalchemy/sql/schema.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 4ed5b9e6b..3320214a2 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2241,9 +2241,6 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): if isinstance(type_, SchemaEventTarget): type_ = type_.copy(**kw) - if self._user_defined_nullable is not NULL_UNSPECIFIED: - column_kwargs["nullable"] = self._user_defined_nullable - # TODO: DefaultGenerator is not copied here! it's just used again # with _set_parent() pointing to the old column. see the new # use of _copy() in the new _merge() method @@ -2267,6 +2264,12 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): *args, **column_kwargs, ) + + # copy the state of "nullable" exactly, to accommodate for + # ORM flipping the .nullable flag directly + c.nullable = self.nullable + c._user_defined_nullable = self._user_defined_nullable + return self._schema_item_copy(c) def _merge(self, other: Column[Any]) -> None: @@ -2300,6 +2303,7 @@ class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]): and other._user_defined_nullable is NULL_UNSPECIFIED ): other.nullable = self.nullable + other._user_defined_nullable = self._user_defined_nullable if self.default is not None and other.default is None: new_default = self.default._copy() -- cgit v1.2.1