Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I've used DBIx::Class to access some similar tables. Some of the methods around a table relationship puzzled me. On one of my tables, it worked but on the other one it didn't. Schema below (Postgres):
CREATE TABLE r1 ( id integer, name character varying(64), PRIMARY KEY (id) ); CREATE TABLE t1 ( id integer, groupnames integer REFERENCES r1(id), PRIMARY KEY (id) ); CREATE TABLE r2 ( id integer, name character varying(64), PRIMARY KEY (id) ); CREATE TABLE t2 ( id integer, groupname integer REFERENCES r2(id), PRIMARY KEY (id) );
So when I use dbicdump to generate my classes, everything seems to be fine, the has_many callers are mapped properly. Despite the fields called groupnames (t1) and groupname (t2), the relationship mapping only works with groupname. When using groupnames, the method will load the identifier and not the relationship value. I probably overlooked something in the DBIx::Class documentation about the naming conventions for table relationships. Since I cracked my head on this for a few days, thought I'd share this so others can avoid the same pitfalls.
use lib qw(lib); use DBIC::Schema; use Data::Dumper; my $schema = DBIC::Schema->connect("dbi:Pg:dbname=dbic; host=localhost +","pguser","pgpassword"); my $t1_rs = $schema->resultset('T1'); $query_rs = $t1_rs->search; while (my $account = $query_rs->next) { print Dumper $account->groupname->name; } my $t2_rs = $schema->resultset('T2'); $query_rs = $t2_rs->search; while (my $account = $query_rs->next) { print Dumper $account->groupname->name; }

... I'm belgian but I don't play one on TV.

In reply to DBI::Class relationship naming by Beatnik

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (4)
    As of 2020-02-29 03:38 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (128 votes). Check out past polls.