Re: Deleting an object on the other side of a has_a

[prev] [thread] [next] [Date index for 2004/09/01]

From: William McKee
Subject: Re: Deleting an object on the other side of a has_a
Date: 16:45 on 01 Sep 2004
On Wed, Sep 01, 2004 at 12:33:39PM -0400, Perrin Harkins wrote:
> I was referring to automatically deleting b when you remove the
> relationship from a.  It could be an after_update trigger.

OK, let me see if I understand. In the after_update trigger, you would
add code to see if $self->my_b was undef or empty then delete b. How
would you know which b to delete if we've removed the reference?


> > It seems that CDBI is not fully supporting removal of child(?) objects
> > in a relationship. Perhaps, it would be better to reload a after
> > deleting b? It seems that CDBI should handle that in the background, no?
> 
> Well, deleting object b while object a still references it is a bug in
> your code.  You aren't supposed to do that, and databases with
> referential integrity checks won't allow you to.

Absolutely. Let me put it in the context of a might_have relationship
where a might_have b. Say the user no longer wants liner_notes (to use
the CDBI example). I need a way to delete the object. I think Peter's
example along with your suggestion to update a before deleting b appears
to be the best solution.

In reviewing my code again, I see that I've really only skirted the
issue. Instead of using the longer version that Peter showed, I'm using
the following format:

  $a->my_b->delete();

I believe that this could introduce the referential integrity problem. I
have not seen it b/c I don't have referential integrity enabled on my
tables. I'm going to change that one-liner to the following:

   my $b = $self->my_b;
   $self->my_b(undef);
   $a->update;
   $b->delete;

Does that look right to you? If so, I will update the wiki with a
synopsis of this discussion.


Thanks,
William

        -- 
        Knowmad Services Inc.
http://www.knowmad.com

(message missing)

Deleting an object on the other side of a has_a
Peter Pimley 10:02 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
Perrin Harkins 16:09 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
Perrin Harkins 15:32 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
Perrin Harkins 16:33 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
William McKee 16:45 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
Perrin Harkins 17:03 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
Perrin Harkins 16:42 on 01 Sep 2004

Re: Deleting an object on the other side of a has_a
Perrin Harkins 15:06 on 02 Sep 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52