1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

[6.0.x] Fixed #36611, Refs #36580 -- Added system check for multicolumn ForeignObject in Meta.indexes/constraints/unique_together.

ForeignObjects with multiple `from_fields` are not supported in these
options.

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>

Backport of 5b51e6f759 from main.
This commit is contained in:
Sarah Boyce
2025-09-19 16:27:51 +02:00
committed by Natalia
parent 86e7406cfb
commit 0fa339ce71
4 changed files with 69 additions and 0 deletions

View File

@@ -166,6 +166,54 @@ class UniqueTogetherTests(SimpleTestCase):
],
)
def test_pointing_to_foreign_object(self):
class Reference(models.Model):
reference_id = models.IntegerField(unique=True)
class ReferenceTab(models.Model):
reference_id = models.IntegerField()
reference = models.ForeignObject(
Reference,
from_fields=["reference_id"],
to_fields=["reference_id"],
on_delete=models.CASCADE,
)
class Meta:
unique_together = [["reference"]]
self.assertEqual(ReferenceTab.check(), [])
def test_pointing_to_foreign_object_multi_column(self):
class Reference(models.Model):
reference_id = models.IntegerField(unique=True)
code = models.CharField(max_length=1)
class ReferenceTabMultiple(models.Model):
reference_id = models.IntegerField()
code = models.CharField(max_length=1)
reference = models.ForeignObject(
Reference,
from_fields=["reference_id", "code"],
to_fields=["reference_id", "code"],
on_delete=models.CASCADE,
)
class Meta:
unique_together = [["reference"]]
self.assertEqual(
ReferenceTabMultiple.check(),
[
Error(
"'unique_together' refers to a ForeignObject 'reference' with "
"multiple 'from_fields', which is not supported for that option.",
obj=ReferenceTabMultiple,
id="models.E049",
),
],
)
@isolate_apps("invalid_models_tests")
class IndexesTests(TestCase):