Adjusted the :class:`_types.Enum` datatype to accept an argument of
``None`` for the :paramref:`_types.Enum.length` parameter, resulting in a
VARCHAR or other textual type with no length in the resulting DDL. This
allows for new elements of any length to be added to the type after it
exists in the schema. Pull request courtesy Eugene Toder.
Fixes: #10269
Closes: #10274
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10274
Pull-request-sha:
651afaaea76c1ec868402cdd7106ec8b2de76254
Change-Id: I374ae9e5fa63da21a4cc87e323ce1a54acd6e39b
--- /dev/null
+.. change::
+ :tags: usecase, sql
+ :tickets: 10269
+
+ Adjusted the :class:`_types.Enum` datatype to accept an argument of
+ ``None`` for the :paramref:`_types.Enum.length` parameter, resulting in a
+ VARCHAR or other textual type with no length in the resulting DDL. This
+ allows for new elements of any length to be added to the type after it
+ exists in the schema. Pull request courtesy Eugene Toder.
+
self._default_length = length = 0
if length_arg is not NO_ARG:
- if not _disable_warnings and length_arg < length:
+ if (
+ not _disable_warnings
+ and length_arg is not None
+ and length_arg < length
+ ):
raise ValueError(
"When provided, length must be larger or equal"
" than the length of the longest enum value. %s < %s"
)
def as_generic(self, allow_nulltype=False):
- if hasattr(self, "enums"):
+ try:
args = self.enums
- else:
+ except AttributeError:
raise NotImplementedError(
"TypeEngine.as_generic() heuristic "
"is undefined for types that inherit Enum but do not have "
"an `enums` attribute."
- )
+ ) from None
return util.constructor_copy(
self, self._generic_type_affinity, *args, _disable_warnings=True
e = Enum("x", "y", "long", native_enum=False, length=42)
eq_(e.length, 42)
+ def test_none_length_non_native(self):
+ e = Enum("x", "y", native_enum=False, length=None)
+ eq_(e.length, None)
+ eq_(repr(e), "Enum('x', 'y', native_enum=False, length=None)")
+ self.assert_compile(e, "VARCHAR", dialect="default")
+
def test_omit_aliases(self, connection):
table0 = self.tables["stdlib_enum_table"]
type0 = table0.c.someenum.type