Skip to content

PhoneNumberType: missing type coercion when binding params for a LIKE statement #560

@Ayak973

Description

@Ayak973

When we want to use a filter on a column of type PhonenumberType with a like statement, the default behavior produce an SQL
query with a phonenumber constructed with default US region:

SQL produced (sqlalchemy engine echo on):
SELECT phone_number.phone_number FROM phone_number WHERE phone_number.phone_number LIKE %s

Bound values produced (incorrect):
('+1456')

Bound values expected (correct):
('%456%')

Possible workaround: make the type coercion return a string for the like (and notlike) statements. We have subclassed PhonenumberType to bypass this limitation (inspired by the original function from SQLAlchemy):

class PhoneNumberType(sqlalchemy_utils.types.phone_number.PhoneNumberType):
    def coerce_compared_value(self, op, value):
        if op in (like_op, notlike_op):
            return String()
        else:
            return self

Is it possible to add this behavior to the library, or have we missed something ?

OS: Debian 11 bullseye
Python: 3.9.2
SQLAlchemy_utils: 0.37.9

Regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions