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;


In reply to Re: Class::DBI cascading delete problem? by gryphon
in thread Class::DBI cascading delete problem? by zigdon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.