﻿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
34007	Single-field conditional UniqueContraint validation errors are classified as non-field-errors	David Sanders	David Sanders	"Mailing list discussion with Simon Charette: https://groups.google.com/g/django-developers/c/7QGy6i9oe4g/m/iCJE7be0AgAJ

Validating models with UniqueConstraint usually groups errors by field name:

{{{
class Test(Model):
    test = CharField(max_length=255)

    class Meta:
        constraints = [
            UniqueConstraint(fields=[""test""], name=""unique""),
        ]

Test.objects.create(test=""abc"")
test = Test(test=""abc"")
test.validate_constraints()
django.core.exceptions.ValidationError: {'test': ['Test with this Test already exists.']}
}}}

However if a condition is added to the UniqueConstraint the validation error is categorised as a non-field error:

{{{
class Test(Model):
    test = CharField(max_length=255)

    class Meta:
        constraints = [
            UniqueConstraint(fields=[""test""], name=""unique"", condition=~Q(test="""")),
        ]

Test.objects.create(test=""abc"")
test = Test(test=""abc"")
test.validate_constraints()
django.core.exceptions.ValidationError: {'__all__': ['Constraint “unique” is violated.']}
}}}

My proposal is to make the validation error a field-error for any unique constraint that only has a single field defined.

This affects the way custom error messages are defined (eg forms error_messages dict vs constraint violation_error_message).

It's been agreed that the error message wording should remain the generic ""Constraint is violated"" format as determining the correct wording involving conditions is non-trivial."	Cleanup/optimization	assigned	Database layer (models, ORM)	4.1	Normal				Accepted	1	0	0	1	0	0
