mirror of
https://github.com/django/django.git
synced 2025-07-12 07:45:11 +00:00
Fixed #35234 -- Added system checks for invalid model field names in ExclusionConstraint.expressions.
This commit is contained in:
parent
0fb104dda2
commit
f82c67aa21
2 changed files with 64 additions and 0 deletions
|
@ -2,12 +2,17 @@ import datetime
|
|||
from unittest import mock
|
||||
|
||||
from django.contrib.postgres.indexes import OpClass
|
||||
from django.core.checks import Error
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError, NotSupportedError, connection, transaction
|
||||
from django.db.models import (
|
||||
CASCADE,
|
||||
CharField,
|
||||
CheckConstraint,
|
||||
DateField,
|
||||
Deferrable,
|
||||
F,
|
||||
ForeignKey,
|
||||
Func,
|
||||
IntegerField,
|
||||
Model,
|
||||
|
@ -328,6 +333,57 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
|
|||
include="invalid",
|
||||
)
|
||||
|
||||
@isolate_apps("postgres_tests")
|
||||
def test_check(self):
|
||||
class Author(Model):
|
||||
name = CharField(max_length=255)
|
||||
alias = CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
app_label = "postgres_tests"
|
||||
|
||||
class Book(Model):
|
||||
title = CharField(max_length=255)
|
||||
published_date = DateField()
|
||||
author = ForeignKey(Author, CASCADE)
|
||||
|
||||
class Meta:
|
||||
app_label = "postgres_tests"
|
||||
constraints = [
|
||||
ExclusionConstraint(
|
||||
name="exclude_check",
|
||||
expressions=[
|
||||
(F("title"), RangeOperators.EQUAL),
|
||||
(F("published_date__year"), RangeOperators.EQUAL),
|
||||
("published_date__month", RangeOperators.EQUAL),
|
||||
(F("author__name"), RangeOperators.EQUAL),
|
||||
("author__alias", RangeOperators.EQUAL),
|
||||
("nonexistent", RangeOperators.EQUAL),
|
||||
],
|
||||
)
|
||||
]
|
||||
|
||||
self.assertCountEqual(
|
||||
Book.check(databases=self.databases),
|
||||
[
|
||||
Error(
|
||||
"'constraints' refers to the nonexistent field 'nonexistent'.",
|
||||
obj=Book,
|
||||
id="models.E012",
|
||||
),
|
||||
Error(
|
||||
"'constraints' refers to the joined field 'author__alias'.",
|
||||
obj=Book,
|
||||
id="models.E041",
|
||||
),
|
||||
Error(
|
||||
"'constraints' refers to the joined field 'author__name'.",
|
||||
obj=Book,
|
||||
id="models.E041",
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
def test_repr(self):
|
||||
constraint = ExclusionConstraint(
|
||||
name="exclude_overlapping",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue