cansadadeserfeliz
cansadadeserfeliz

Reputation: 3133

Django: Sqlite and regex in query

I'm using Django with a PostgreSQL database and this query works fine:

REQUEST_TYPE_ENTRANCE = 1
REGEX_ENTRANCE = r'^{0},|,{0},|,{0}$|^{0}$'.format(
    REQUEST_TYPE_ENTRANCE
)
entrance_registers = EntranceRegister.objects.filter(authorized_requests__regex=REGEX_ENTRANCE)

But when I run tests with SQLite database it returns no results.

Here is the output of print EntranceRegister.objects.filter(authorized_requests__regex=REGEX_ENTRANCE).query:

SELECT "porter_entranceregister"."id", "porter_entranceregister"."status", "porter_entranceregister"."authorized_requests", "porter_entranceregister"."gone_at", "porter_entranceregister"."created_at" 
FROM "porter_entranceregister" 
WHERE "porter_entranceregister"."authorized_requests" 
REGEXP ^1,|,1,|,1$|^1$  ORDER BY "porter_entranceregister"."created_at" 
DESC

I suppose that the problem might be with missing quotes here: https://github.com/django/django/blob/master/django/db/backends/sqlite3/base.py#L129, but when I change this file in my virtualenv, it fails with

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 6, and there are 7 supplied.

Upvotes: 2

Views: 698

Answers (1)

CL.
CL.

Reputation: 180210

SQLite does not implement the REGEXP operator by default. (And if it were, the second operand would have to by a string.)

You can get the same effect with LIKE:

... WHERE reqs LIKE '1,%' OR reqs LIKE '%,1,%' OR reqs LIKE '%,1' OR reqs = '1'

Upvotes: 1

Related Questions