diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-20 10:08:36 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-20 11:45:24 -0400 |
| commit | 91a1022227499c8efce038c4a0a9bdcdb14a69d0 (patch) | |
| tree | 8911d31a6c3a15731038211d9e8908c5dcdb2a2e /lib/sqlalchemy/sql | |
| parent | 4e811fd72e615116c448fba323aa57f7efd4798c (diff) | |
| download | sqlalchemy-91a1022227499c8efce038c4a0a9bdcdb14a69d0.tar.gz | |
Disable Enum string validation by default
Rolled back the validation rules a bit in :class:`.Enum` to allow
unknown string values to pass through, unless the flag
``validate_string=True`` is passed to the Enum; any other kind of object is
still of course rejected. While the immediate use
is to allow comparisons to enums with LIKE, the fact that this
use exists indicates there may be more unknown-string-comparsion use
cases than we expected, which hints that perhaps there are some
unknown string-INSERT cases too.
Change-Id: I7d1d79b374a7d47966d410998f77cd19294ab7b0
Fixes: #3725
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/sqltypes.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 166e61822..977231336 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -24,6 +24,7 @@ from . import operators from .. import inspection from .. import event from ..util import pickle +from ..util import compat import decimal if util.jython: @@ -1205,6 +1206,11 @@ class Enum(String, SchemaType): ``schema`` attribute. This also takes effect when using the :meth:`.Table.tometadata` operation. + :param validate_strings: when True, invalid string values will + be validated and not be allowed to pass through. + + .. versionadded:: 1.1.0b2 + """ values, objects = self._parse_into_values(enums, kw) @@ -1213,6 +1219,8 @@ class Enum(String, SchemaType): self.native_enum = kw.pop('native_enum', True) convert_unicode = kw.pop('convert_unicode', None) self.create_constraint = kw.pop('create_constraint', True) + self.validate_strings = kw.pop('validate_strings', False) + if convert_unicode is None: for e in self.enums: if isinstance(e, util.text_type): @@ -1262,8 +1270,20 @@ class Enum(String, SchemaType): try: return self._valid_lookup[elem] except KeyError: - raise LookupError( - '"%s" is not among the defined enum values' % elem) + # for unknown string values, we return as is. While we can + # validate these if we wanted, that does not allow for lesser-used + # end-user use cases, such as using a LIKE comparison with an enum, + # or for an application that wishes to apply string tests to an + # ENUM (see [ticket:3725]). While we can decide to differentiate + # here between an INSERT statement and a criteria used in a SELECT, + # for now we're staying conservative w/ behavioral changes (perhaps + # someone has a trigger that handles strings on INSERT) + if not self.validate_strings and \ + isinstance(elem, compat.string_types): + return elem + else: + raise LookupError( + '"%s" is not among the defined enum values' % elem) def _object_value_for_elem(self, elem): try: @@ -1314,6 +1334,7 @@ class Enum(String, SchemaType): convert_unicode=self.convert_unicode, native_enum=self.native_enum, inherit_schema=self.inherit_schema, + validate_strings=self.validate_strings, _create_events=_create_events, *args, **kw) |
