Opened 15 hours ago

Last modified 15 hours ago

#36594 new Cleanup/optimization

Clarify UniqueConstraint's nulls_distinct system check warning

Reported by: Russell Owen Owned by:
Component: Database layer (models, ORM) Version: 5.2
Severity: Normal Keywords: UniqueConstraint nulls_distinct
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
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 (2)

comment:1 by Russell Owen, 15 hours ago

Description: modified (diff)

comment:2 by Tim Graham, 15 hours 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.

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