Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17519 -- Fixed missing SQL constraints to proxy models.

Thanks thibaultj for the report, jenh for the patch,
and charettes for the tests.
  • Loading branch information...
commit aa830009de940e31711b4858501becd7f34a1972 1 parent 31ee120
Tim Graham authored
2  django/db/backends/creation.py
@@ -146,7 +146,7 @@ def sql_for_pending_references(self, model, style, pending_references):
146 146
         Returns any ALTER TABLE statements to add constraints after the fact.
147 147
         """
148 148
         opts = model._meta
149  
-        if not opts.managed or opts.proxy or opts.swapped:
  149
+        if not opts.managed or opts.swapped:
150 150
             return []
151 151
         qn = self.connection.ops.quote_name
152 152
         final_output = []
7  tests/backends/models.py
@@ -68,11 +68,18 @@ def __str__(self):
68 68
         return "%s %s" % (self.first_name, self.last_name)
69 69
 
70 70
 
  71
+class ReporterProxy(Reporter):
  72
+    class Meta:
  73
+        proxy = True
  74
+
  75
+
71 76
 @python_2_unicode_compatible
72 77
 class Article(models.Model):
73 78
     headline = models.CharField(max_length=100)
74 79
     pub_date = models.DateField()
75 80
     reporter = models.ForeignKey(Reporter)
  81
+    reporter_proxy = models.ForeignKey(ReporterProxy, null=True,
  82
+                                       related_name='reporter_proxy')
76 83
 
77 84
     def __str__(self):
78 85
         return self.headline
39  tests/backends/tests.py
@@ -614,12 +614,19 @@ def test_integrity_checks_on_creation(self):
614 614
         Try to create a model instance that violates a FK constraint. If it
615 615
         fails it should fail with IntegrityError.
616 616
         """
617  
-        a = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
  617
+        a1 = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30)
618 618
         try:
619  
-            a.save()
  619
+            a1.save()
620 620
         except IntegrityError:
621  
-            return
622  
-        self.skipTest("This backend does not support integrity checks.")
  621
+            pass
  622
+        else:
  623
+            self.skipTest("This backend does not support integrity checks.")
  624
+        # Now that we know this backend supports integrity checks we make sure
  625
+        # constraints are also enforced for proxy models. Refs #17519
  626
+        a2 = models.Article(headline='This is another test', reporter=self.r,
  627
+                            pub_date=datetime.datetime(2012, 8, 3),
  628
+                            reporter_proxy_id=30)
  629
+        self.assertRaises(IntegrityError, a2.save)
623 630
 
624 631
     def test_integrity_checks_on_update(self):
625 632
         """
@@ -628,14 +635,26 @@ def test_integrity_checks_on_update(self):
628 635
         """
629 636
         # Create an Article.
630 637
         models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r)
631  
-        # Retrive it from the DB
632  
-        a = models.Article.objects.get(headline="Test article")
633  
-        a.reporter_id = 30
  638
+        # Retrieve it from the DB
  639
+        a1 = models.Article.objects.get(headline="Test article")
  640
+        a1.reporter_id = 30
634 641
         try:
635  
-            a.save()
  642
+            a1.save()
636 643
         except IntegrityError:
637  
-            return
638  
-        self.skipTest("This backend does not support integrity checks.")
  644
+            pass
  645
+        else:
  646
+            self.skipTest("This backend does not support integrity checks.")
  647
+        # Now that we know this backend supports integrity checks we make sure
  648
+        # constraints are also enforced for proxy models. Refs #17519
  649
+        # Create another article
  650
+        r_proxy = models.ReporterProxy.objects.get(pk=self.r.pk)
  651
+        models.Article.objects.create(headline='Another article',
  652
+                                      pub_date=datetime.datetime(1988, 5, 15),
  653
+                                      reporter=self.r, reporter_proxy=r_proxy)
  654
+        # Retreive the second article from the DB
  655
+        a2 = models.Article.objects.get(headline='Another article')
  656
+        a2.reporter_proxy_id = 30
  657
+        self.assertRaises(IntegrityError, a2.save)
639 658
 
640 659
     def test_disable_constraint_checks_manually(self):
641 660
         """

0 notes on commit aa83000

Please sign in to comment.
Something went wrong with that request. Please try again.