Beatnik has asked for the wisdom of the Perl Monks concerning the following question:
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):
Greetz
Beatnik
... I'm belgian but I don't play one on TV.
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.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) );
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; }
Greetz
Beatnik
... I'm belgian but I don't play one on TV.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: DBI::Class relationship naming
by shmem (Chancellor) on Dec 31, 2016 at 16:02 UTC | |
by Beatnik (Parson) on Jan 01, 2017 at 08:06 UTC | |
by soonix (Canon) on Jan 01, 2017 at 10:20 UTC | |
Re: DBI::Class relationship naming
by Beatnik (Parson) on Jan 01, 2017 at 10:59 UTC | |
by soonix (Canon) on Jan 01, 2017 at 19:27 UTC | |
by Beatnik (Parson) on Jan 01, 2017 at 19:47 UTC | |
by soonix (Canon) on Jan 02, 2017 at 21:53 UTC | |
by Beatnik (Parson) on Jan 03, 2017 at 21:10 UTC | |
Re: DBI::Class relationship naming
by Mr. Muskrat (Canon) on Jan 03, 2017 at 23:02 UTC | |
by Beatnik (Parson) on Jan 04, 2017 at 20:25 UTC |
Back to
Seekers of Perl Wisdom