Opened 3 weeks ago

Last modified 6 days ago

#36594 assigned Cleanup/optimization

Clarify UniqueConstraint's nulls_distinct system check warning

Reported by: Russell Owen Owned by: Mridul
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords: UniqueConstraint nulls_distinct
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description (last modified by Russell Owen)

I was trying to create a UniqueConstraint with nulls_distinct=False using sqlite. When I migrated my sqlite database I got this warning:

SQLite does not support unique constraints with nulls distinct.

That message implies that the value of False should work, so it is misleading.

It turns out that boolean values of nulls_distinct are only acceptable for postgresql, which I missed when I first read the docs. It would be very helpful if the warning message included the information that boolean values of nulls_distinct are not acceptable for sqlite (or whatever database is triggering the warning) so the user knows how to fix the problem.

Having the warning also include the information about the actual behavior is a nice touch, but is not the heart of the problem.

Here is the simplest model I could come up with that shows the issue:

class SimpleModel(models.Model):
    field_a = models.IntegerField(null=True)
    field_b = models.IntegerField(null=True)

    class Meta:
        constraints = [
            UniqueConstraint(
                name="simple_unique_constraint",
                fields=["field_a", "field_b"],
                nulls_distinct=False,
            ),
        ]

(The same warning is printed whether nulls_distinct is False or True, which is why it is so confusing.)

It would be also nice to emphasize the limitation of nulls_distinct in the documentation by making it warning or putting it in bold as the first line of the description of the argument.

Change History (9)

comment:1 by Russell Owen, 3 weeks ago

Description: modified (diff)

comment:2 by Tim Graham, 3 weeks ago

Easy pickings: set
Summary: UniqueConstraint with a boolean value of nulls_distinct causes misleading warning for sqlite (and likely other dbs)Clarify UniqueConstraint's nulls_distinct system check warning
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

See #36592 for initial discussion.

comment:3 by Mridul, 3 weeks ago

Owner: set to Mridul
Status: newassigned

comment:4 by Samriddha Kumar Tripathi, 2 weeks ago

I’ve opened a pull request for this:
https://github.com/django/django/pull/19839

comment:5 by Samriddha Kumar Tripathi, 2 weeks ago

Has patch: set

comment:6 by Mridul, 2 weeks ago

Has patch: unset

Hi Samriddha,
I just noticed that you submitted a PR for this ticket, which I had already assigned to myself and have been working on. In the future, please make sure to check the assignment status of tickets before starting work

Also, the PR doesn’t include the required documentation changes noted in the ticket. I request you to hold off on further work and let me finish it from here

Last edited 2 weeks ago by Mridul (previous) (diff)

comment:7 by Mridul, 10 days ago

Will have a PR ready before the end of this week

comment:9 by Sarah Boyce, 6 days ago

Patch needs improvement: set
Note: See TracTickets for help on using tickets.
Back to Top