Looks like we do need a delete rules chapter in the Modeler Guide
rather sooner than later. There is indeed some confusion. And hopefully
this discussion will help me formulate this better.
On Dec 12, 2003, at 10:54 AM, Jim Menard wrote:
> I've been reading the "Problems understanding delete behavior" thread
> in the archives and I would like to clarify my understanding. (I can't
> reply directly to the thread because of the spam block.) I'd like to
> talk through a couple of scenarios to make sure I understand what's
> happening and how to use delete rules properly.
>
> Does the delete rule take effect when the source gets deleted or when
> the target gets deleted? Based on Andrus's replies, I think it's the
> source.
Correct.
> Using Mark's example,
>
> TestCategory +----<< TestGroup +----<< TestSubject
>
> TestSubject has a many-to-one relationship with TestGroup. If I set
> the TestGroup's toTestSubjectArray relationship's delete rule to
> "NULLIFY", then when I call context.deleteObject(subject) the "source"
> is the group and the "target" is the subject. (How is group the
> "source"? The group is not mentioned at all except indirectly because
> the subject has a FK to the group.)
No, in your example Subject is the "source".
Relationships in Cayenne are uni-directional, so delete rule for
relationship going from TestGroup to TestSubject is independent from
the delete rule going in the other direction - from TestSubject to
TestGroup, since those are two independent relationships. In this case
"toTestSubjectArray" rule won't be applied when deleting a subject.
Rather a rule for "toTestGroup" is used. This explanation may answer
some of the questions below too.
> Here is where I am confused: there is nothing to nullify in the
> database. If we remove the subject record, it was the table with the
> foreign key back to the group table and therefore the removal of the
> subject record has no effect on the group table at all. In this case,
> does "NULLIFY" mean something to Cayenne (but not to the database
> data)?
Yes, the rules exist mainly for object graph maintenance, and work in
terms of objects. Applying a given rule may or may not result in a
database row update. My earlier explanation using FK/PK was just to
demonstrate how things can go wrong at the very bottom as a result of
an incorrect rule, but this is just part of the picture.
So in this example "Nullify" rule on "toTestSubjectArray" relationship
(applied when deleting TestGroups) will set a Group reference of all
its former Subjects to null, resulting in later database save error. On
the other hand "Nullify" rule on "toTestGroup" relationship will remove
a subject from the Group's subject array when a subject is deleted.
This will not result in any database update to Group, but will maintain
Groups's subject array in a consistent state after the subject is
deleted.
Andrus
This archive was generated by hypermail 2.0.0 : Fri Dec 12 2003 - 20:36:45 EST