mirror of
https://github.com/django/django.git
synced 2025-10-28 16:16:12 +00:00
[1.9.x] Fixed #25535 -- Made ForeignObject checks less strict.
Check that the foreign object `from_fields` are a subset of any unique constraints on the foreign model. Backport of80dac8c33eandc7aff31397from master
This commit is contained in:
committed by
Tim Graham
parent
aa0a3b680e
commit
38d6e1e2ad
@@ -2,7 +2,9 @@ import datetime
|
||||
from operator import attrgetter
|
||||
|
||||
from django.core.exceptions import FieldError
|
||||
from django.test import TestCase, skipUnlessDBFeature
|
||||
from django.db import models
|
||||
from django.db.models.fields.related import ForeignObject
|
||||
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
|
||||
from django.utils import translation
|
||||
|
||||
from .models import (
|
||||
@@ -391,3 +393,52 @@ class MultiColumnFKTests(TestCase):
|
||||
""" See: https://code.djangoproject.com/ticket/21566 """
|
||||
objs = [Person(name="abcd_%s" % i, person_country=self.usa) for i in range(0, 5)]
|
||||
Person.objects.bulk_create(objs, 10)
|
||||
|
||||
|
||||
class TestModelCheckTests(SimpleTestCase):
|
||||
|
||||
def test_check_composite_foreign_object(self):
|
||||
class Parent(models.Model):
|
||||
a = models.PositiveIntegerField()
|
||||
b = models.PositiveIntegerField()
|
||||
|
||||
class Meta:
|
||||
unique_together = (('a', 'b'),)
|
||||
|
||||
class Child(models.Model):
|
||||
a = models.PositiveIntegerField()
|
||||
b = models.PositiveIntegerField()
|
||||
value = models.CharField(max_length=255)
|
||||
parent = ForeignObject(
|
||||
Parent,
|
||||
on_delete=models.SET_NULL,
|
||||
from_fields=('a', 'b'),
|
||||
to_fields=('a', 'b'),
|
||||
related_name='children',
|
||||
)
|
||||
|
||||
self.assertEqual(Child._meta.get_field('parent').check(from_model=Child), [])
|
||||
|
||||
def test_check_subset_composite_foreign_object(self):
|
||||
class Parent(models.Model):
|
||||
a = models.PositiveIntegerField()
|
||||
b = models.PositiveIntegerField()
|
||||
c = models.PositiveIntegerField()
|
||||
|
||||
class Meta:
|
||||
unique_together = (('a', 'b'),)
|
||||
|
||||
class Child(models.Model):
|
||||
a = models.PositiveIntegerField()
|
||||
b = models.PositiveIntegerField()
|
||||
c = models.PositiveIntegerField()
|
||||
d = models.CharField(max_length=255)
|
||||
parent = ForeignObject(
|
||||
Parent,
|
||||
on_delete=models.SET_NULL,
|
||||
from_fields=('a', 'b', 'c'),
|
||||
to_fields=('a', 'b', 'c'),
|
||||
related_name='children',
|
||||
)
|
||||
|
||||
self.assertEqual(Child._meta.get_field('parent').check(from_model=Child), [])
|
||||
|
||||
Reference in New Issue
Block a user