Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Class::DBI cascading delete problem?

by gryphon (Abbot)
on Sep 27, 2004 at 16:39 UTC ( #394239=note: print w/replies, xml ) Need Help??


in reply to Class::DBI cascading delete problem?

Greetings zigdon,

I'm still learning CBDI myself, so I don't know how helpful this will be. However, here's what I've come up with. I think the problem is with your has_many definitions for Org and PartType. The two lines should be more like:

__PACKAGE__->has_many('PartTypes' => 'MyCDBI::PartType', 'typeid');

To solve the problem, I had to rewrite ever so slightly some of your conventions (to help me mentally understand things because I'm still learning CDBI myself), so you'll want to translate this code back. However, here's the final code that does appear to work for me...

#!/usr/bin/perl use strict; use warnings; package MyCDBI; use base 'Class::DBI::mysql'; __PACKAGE__->connection('dbi:mysql:cdbi', 'gryphon', 'PASSWORD'); package MyCDBI::Org; use base 'MyCDBI'; __PACKAGE__->set_up_table('org'); __PACKAGE__->has_many('PartTypes' => 'MyCDBI::PartType', 'typeid'); package MyCDBI::PartType; use base 'MyCDBI'; __PACKAGE__->set_up_table('parttype'); __PACKAGE__->has_a('orgid' => 'MyCDBI::Org'); __PACKAGE__->has_many('PartTypes' => 'MyCDBI::OrgPart', 'typeid'); package MyCDBI::OrgPart; use base 'MyCDBI'; __PACKAGE__->set_up_table('orgpart'); __PACKAGE__->has_a('typeid' => 'MyCDBI::PartType'); package main; my $org = MyCDBI::Org->create({ orgid => 1 }); my $pt1 = MyCDBI::PartType->create({ typeid => 1, orgid => $org }); my $pt2 = MyCDBI::PartType->create({ typeid => 2, orgid => $org }); my $op1 = MyCDBI::OrgPart->create({ typeid => 1, typeid => $pt1 }); my $op2 = MyCDBI::OrgPart->create({ typeid => 2, typeid => $pt1 }); my $op3 = MyCDBI::OrgPart->create({ typeid => 3, typeid => $pt2 }); my $op4 = MyCDBI::OrgPart->create({ typeid => 4, typeid => $pt2 }); $org->delete;

And just to make sure I cover my assumptions, here's the create SQL for the database...

CREATE TABLE org ( orgid tinyint(3) unsigned NOT NULL auto_increment, PRIMARY KEY (orgid) ) TYPE=MyISAM; CREATE TABLE orgpart ( opid tinyint(3) unsigned NOT NULL auto_increment, typeid tinyint(3) unsigned default '0', PRIMARY KEY (opid) ) TYPE=MyISAM; CREATE TABLE parttype ( typeid tinyint(3) unsigned NOT NULL auto_increment, orgid tinyint(3) unsigned default '0', PRIMARY KEY (typeid) ) TYPE=MyISAM;

I hope this helps. I'm still struggling with CDBI in hopes that eventually it will be worth it.

gryphon
code('Perl') || die;

Replies are listed 'Best First'.
Re^2: Class::DBI cascading delete problem?
by zigdon (Deacon) on Sep 27, 2004 at 18:54 UTC
    __PACKAGE__->has_many('PartTypes' => 'MyCDBI::PartType', 'typeid');
    Hmm. Either I'm confused, or you are? This means (in the Org package), that the PartType is storing the orgid key in the 'typeid' column? Which is not the same as saying
    __PACKAGE__->has_many('PartTypes' => ['MyCDBI::PartType', 'typeid']);
    Which means
    return map { $_->typeid } $org->PartTypes;
    Or am I confused?

    -- zigdon

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://394239]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2020-12-01 15:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (11 votes). Check out past polls.

    Notices?