godzirra has asked for the wisdom of the Perl Monks concerning the following question:

I just started with Class::DBI and I'm having some issues.

The problem I'm having is that when I run the following: ./dupe_charts.pl 74

I get the following error:

chart is not a column of CCopy::Chart_Item_XREF at /usr/local/share/perl/5.8.4/Class/DBI/Relationship/HasMany.pm line 118

#### Class dbi instance script ###### use strict; use CCopy; my $owner_id = shift || die "Must call with a site id to copy to."; my $it = CCopy::Chart->retrieve_all; while (my $chart = $it->next) { *********** $chart->items; *************** }

#### module ####### package CCopy; use base 'Class::DBI'; CCopy->connection($dsn, $user, $password); package CCopy::Chart; use base 'CCopy'; CCopy::Chart->table('CHART'); CCopy::Chart->columns(Primary => qw/ chart_id /); CCopy::Chart->columns(Essential => qw/ title description owner_id owne +r_type_id chart_type/); CCopy::Chart->has_many(items => 'CCopy::Chart_Item_XREF'); package CCopy::Chart_Item_XREF; use base 'CCopy'; CCopy::Chart_Item_XREF->table('CHART_ITEM_XREF'); CCopy::Chart_Item_XREF->columns(Primary => qw/ chart_id item_id /); CCopy::Chart_Item_XREF->columns(All => qw/ insert_dtime update_dtime / +); ################################

The two tables have these columns:

########### CHART ################## chart_id insert_dtime update_dtime owner_id owner_type_id title description featured chart_type ######## CHART_ITEM_XREF ########### chart_id item_id status_code feature_flag priority_level insert_dtime update_dtime locked_flag special_flag mandatory_flag
Godzirra! Destroying Neo Tokyo since 1954

Replies are listed 'Best First'.
Re: Class DBI Problems
by merlyn (Sage) on May 03, 2005 at 20:57 UTC
    You haven't told has_many which column to use to point back at your charts, so it makes up the moniker "chart", which the XREF table doesn't have. You need to tell it which column to use to get back to your Chart records.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      How do you do that? I looked in the docs, but I'm guessing I missed something, because I'm still not clear on how that works.

      Godzirra! Destroying Neo Tokyo since 1954

        When setting up the relationship we examine the foreign class's + has_a() declarations to discover which of its columns reference our cla +ss. (Note that because this happens at compile time, if the foreign + class is defined in the same file, the class with the has_a() must be + defined earlier than the class with the has_many(). If the classes are +in dif- ferent files, Class::DBI should be able to do the right thing). + If no such has_a() declarations can be found, or none link to us, we +assume that it is linking to us via a column named after the moniker() + of our class. If this is not true you can pass an additional third arg +ument to the has_many() declaration stating which column of the foreign +class references us.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.