Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#29155 closed Bug (fixed)

Using contains lookup with Substr causes modification of second parameter of Substr

Reported by: norac89 Owned by: felixxm
Component: Database layer (models, ORM) Version: 2.0
Severity: Normal Keywords:
Cc: felixxm Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

My model:

class User(models.Model):
    field1 = models.CharField(max_length=120)
    field2 = models.CharField(max_length=120)

Tried querying the model to get User instances in which field2 start with the three first characters of field1.
Tried the following:

User.objects.filter(field2__startswith=Substr(F('field1'), 1, 3))


I m getting the following error

psycopg2.DataError: invalid input syntax for integer: "1%"
LINE 1: ...(REPLACE(REPLACE((SUBSTRING("test_user"."field1", '1%', 3)),...

The second parameter of the substr function seems to be replaced with '1%'.

Attachments (1)

29155-test.diff (1.1 KB) - added by Tim Graham 3 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 3 years ago by Tim Graham

Description: modified (diff)
Summary: Using contains field lookup with Substr database function cause modification of second parameter of SubstrUsing contains lookup with Substr causes modification of second parameter of Substr
Triage Stage: UnreviewedAccepted

I'm attaching a test for Django's test suite that fails (on PostgreSQL but not SQLite) as of ba37ee9ef882deb8e917f1cae0c586a0a275e731.

Changed 3 years ago by Tim Graham

Attachment: 29155-test.diff added

comment:2 Changed 3 years ago by felixxm

Cc: felixxm added

comment:3 Changed 3 years ago by felixxm

Owner: changed from nobody to felixxm
Status: newassigned

comment:4 Changed 3 years ago by felixxm

Has patch: set

comment:5 Changed 3 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:6 Changed 3 years ago by GitHub <noreply@…>

Resolution: fixed
Status: assignedclosed

In feb683c4:

Fixed #29155 -- Fixed crash when database functions are used with pattern lookups.

Thanks Tim Graham and Sergey Fedoseev for reviews.

comment:7 Changed 3 years ago by Tim Graham <timograham@…>

In ba3078c:

Refs #29155 -- Fixed LookupTests.test_pattern_lookups_with_substr() crash on Oracle.

Test introduced in feb683c4c2c5ecfb61e4cb490c3e357450c0c0e8 revealed
unexpected behavior on Oracle that allows concatenating NULL with string.

Note: See TracTickets for help on using tickets.
Back to Top