Re: cascading delete and generic relations

2007-08-07 Thread Jonathan Wood



On Aug 1, 12:30 pm, Jonathan Wood <[EMAIL PROTECTED]> wrote:

>
> Do I need to somehow clean up the "test_group_perms" table when Ideletea 
> subject? Or am I coding my modules incorrectly?

Followup:

After a bit more digging, it looks like Django is not emulating
cascading deletes properly here (SQLite does not support foreign key
constraints so it is up to either my code or Django to do it). We have
implemented some code suggested on the SQLite wiki [1] to maintain
relational integrity, and things are working now.

Jon

[1] http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



cascading delete and generic relations

2007-08-01 Thread Jonathan Wood

I think I am confused about how to use generic relations properly -
when I delete something, cascading delete is not happening as
expected.

Here are my models:

class Permission(models.Model):
name = models.CharField(maxlength=32)
content_type = models.ForeignKey(ContentType, null=True)
object_id = models.PositiveIntegerField(null=True)
content_object = generic.GenericForeignKey()

class Subject(models.Model):
name = models.CharField(maxlength=32)
perms = generic.GenericRelation(Permission)

class Group(models.Model):
name = models.CharField(maxlength=32)
perms = models.ManyToManyField(Permission, null=True)

(The goal is that a group can have permissions, and each permission
pertains to a subject object, but the subject object can be one of
many different types.)

In my test scenario, I add two subjects (s1 and s2), two permissions
referring to these subjects (p1->s1, p2->s2), and one group (g1) that
is assigned both permissions. So far so good.

When I delete the second subject (s2), things mostly happen as
expected:
  Subject.objects.all() shows only s1
  Permission.objects.all() shows only p1
  g1.perms.all() shows only p1

However, the database table that contains the relationships between
Group and Permission still has rows for both permissions (shown using
sqlite3 and .dump):

CREATE TABLE "test_group_perms" (
"id" integer NOT NULL PRIMARY KEY,
"group_id" integer NOT NULL REFERENCES "test_group" ("id"),
"permission_id" integer NOT NULL REFERENCES
"test_permission" ("id"),
UNIQUE ("group_id", "permission_id")
);
INSERT INTO "test_group_perms" VALUES(1, 1, 1);
INSERT INTO "test_group_perms" VALUES(2, 1, 2);
CREATE INDEX "test_permission_content_type_id" ON
"test_permission" ("content_type_id");
COMMIT;

This seems mostly harmless, except that when I create a new subject
(s3) and associate it with a new permission (p3), my group g1 now has
permission p3 even though it was never explicitly assigned that
permission:

>>> g1.perms.all()
[, ]

Do I need to somehow clean up the "test_group_perms" table when I
delete a subject? Or am I coding my modules incorrectly?

I am using the SVN version of Django trunk, updated 7/31/2007.

Thanks for any help!
Jon


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---