Query db.Enum with LIKE filter

Issue #3725 resolved
Leonid Umanskiy created an issue

Hi, I am trying to implement the search functionality over different columns, including Enum. However, when I do:

db.session.query(info_models.ItemProperty).filter(info_models.ItemProperty.grade.like('%'+search_query+'%')).all()

I get an error:

(builtins.LookupError) "%test%" is not among the defined enum values [SQL: '...' ]

The SQL provided runs fine. I am not sure about other database engines, but MySQL supports "LIKE" queries and it's native Enums just fine. I know I can find a work around by comparing the search query with the enum options first, and then include ones that match, but this is dirty and I rather use the functionality that MySQL provides.

Comments (4)

  1. Mike Bayer repo owner

    so this is only in 1.1 beta 1, so if this is for production work you can use 1.0 for now. I can see how this is going to be a problem so I can introduce a typecast that occurs for operators like LIKE.

  2. Mike Bayer repo owner

    will just turn the validation off for strings by default unless there's a flag which is what I wanted in #3095 in the first place.

  3. Mike Bayer repo owner

    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

    → <<cset 91a102222749>>

  4. Log in to comment