﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35329	Bug UniqueConstraint with condition and nulls-distinct	Lucas Lemke	nobody	"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:

{{{#!python
    sql_create_unique_index = (
        ""CREATE UNIQUE INDEX %(name)s ON %(table)s ""
        ""(%(columns)s)%(include)s%(condition)s%(nulls_distinct)s""
    )
}}}

To:
{{{#!python
    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"	Bug	new	Database layer (models, ORM)	5.0	Release blocker		nulls-distinct, condition, UniqueConstraint	Lucas Lemke Simon Charette	Accepted	0	0	0	0	0	0
