Permalink
Browse files

Add a uniqueness constraint to the denormalised fullname model field.

BACKWARDS INCOMPATIBLE CHANGE.
If were using an earlier version of this code, you will need to change
the fullname field in the acacia_topic table to have a unique constraint
(previously, it was indexed, but not required to contain unique values,
which was an error in the design).
  • Loading branch information...
1 parent 36a0315 commit 009f73b46c5c05b113a034e67441eab44e3574e5 Malcolm Tredinnick committed May 17, 2009
Showing with 14 additions and 2 deletions.
  1. +1 −1 acacia/models.py
  2. +13 −1 acacia/tests.py
View
@@ -49,7 +49,7 @@ class AbstractTopic(treebeard_mods.MP_Node):
"""
name = models.CharField(max_length=50)
# Denormalise things a bit so that full name lookups are fast.
- full_name = models.CharField(max_length=512, db_index=True)
+ full_name = models.CharField(max_length=512, unique=True)
node_order_by = ["name"]
separator = "/"
View
@@ -2,7 +2,7 @@
Tests for both the local treebeard overrides and the whole hierarchical topic
structure.
"""
-from django import test
+from django import db, test
import models
@@ -150,6 +150,18 @@ def test_change_parent(self):
self.assertRaises(models.Topic.DoesNotExist,
models.Topic.objects.get_by_full_name, "a/x/c")
+ def test_create_duplicate_entry(self):
+ """
+ Tests that an appropriate error is raised if the tree is manipulated in
+ such a way that a duplicate full name would be created.
+ """
+ target = models.Topic.objects.get_by_full_name("a")
+ node = models.Topic.objects.get_by_full_name("x")
+ # Attempting to move "x" under "a" raises an error at the database
+ # level because we already have an "a/x" node.
+ self.assertRaises(db.IntegrityError, node.move, target,
+ "sorted-child")
+
class TreebeardTests(BaseTestSetup, test.TestCase):
"""
Tests for my local modification/overrides to the default treebeard

0 comments on commit 009f73b

Please sign in to comment.