Opened 8 months ago

Last modified 8 months ago

#35329 closed Bug

Bug UniqueConstraint with condition and nulls-distinct — at Initial Version

Reported by: Lucas Lemke Owned by:
Component: Database layer (models, ORM) Version: 5.0
Severity: Release blocker Keywords: nulls-distinct, condition, UniqueConstraint
Cc: Lucas Lemke, Simon Charette 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

Hi, I`m Lucas (https://github.com/lsaunitti)

I found a bug when set a UniqueConstrain using condition using nulls_distinct using like that:

Screenshot 2024-03-25 at 10.47.59.png

When django generate SQL to create a check constraint the result is "... WHERE <condition> NULLS NOT DISTINCT".
It raise an exception on Postgresql.

To fix it, I suggest change the file django/db/backends/base/schema.py on line 132:

Today:

sql_create_unique_index = (

"CREATE UNIQUE INDEX %(name)s ON %(table)s "
"(%(columns)s)%(include)s%(condition)s%(nulls_distinct)s"

)

To:

sql_create_unique_index = (

"CREATE UNIQUE INDEX %(name)s ON %(table)s "
"(%(columns)s)%(include)s%(nulls_distinct)s%(condition)s"

)

Regards,
Lucas Lemke Saunitti
Software Engineer

Change History (0)

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