:param native_enum: Use the database's native ENUM type when
available. Defaults to True. When False, uses VARCHAR + check
- constraint for all backends.
+ constraint for all backends. The VARCHAR length can be controlled
+ with :paramref:`.Enum.length`
+
+ :param length: Allows specifying a custom length for the VARCHAR
+ when :paramref:`.Enum.native_enum` is False. By default it uses the
+ length of the longest value.
+
+ .. versionadded:: 1.3.16
:param schema: Schema name of this type. For types that exist on the
target database as an independent schema construct (PostgreSQL),
self.create_constraint = kw.pop("create_constraint", True)
self.values_callable = kw.pop("values_callable", None)
self._sort_key_function = kw.pop("sort_key_function", NO_ARG)
+ length_arg = kw.pop("length", NO_ARG)
values, objects = self._parse_into_values(enums, kw)
self._setup_for_values(values, objects, kw)
length = max(len(x) for x in self.enums)
else:
length = 0
+ if not self.native_enum and length_arg is not NO_ARG:
+ if length_arg < length:
+ raise ValueError(
+ "When provided, length must be larger or equal"
+ " than the length of the longest enum value. %s < %s"
+ % (length_arg, length)
+ )
+ length = length_arg
+
self._valid_lookup[None] = self._object_lookup[None] = None
super(Enum, self).__init__(
"inherit_schema=True, native_enum=False)",
)
+ def test_length_native(self):
+ e = Enum("x", "y", "long", length=42)
+
+ eq_(e.length, len("long"))
+
+ # no error is raised
+ e = Enum("x", "y", "long", length=1)
+ eq_(e.length, len("long"))
+
+ def test_length_raises(self):
+ assert_raises_message(
+ ValueError,
+ "When provided, length must be larger or equal.*",
+ Enum,
+ "x",
+ "y",
+ "long",
+ native_enum=False,
+ length=1,
+ )
+
+ def test_no_length_non_native(self):
+ e = Enum("x", "y", "long", native_enum=False)
+ eq_(e.length, len("long"))
+
+ def test_length_non_native(self):
+ e = Enum("x", "y", "long", native_enum=False, length=42)
+ eq_(e.length, 42)
+
binary_table = MyPickleType = metadata = None