[Dbix-class] DBIx::Class:Schema::Loader has_many relationship names

Adam Sjøgren asjo at koldfront.dk
Fri Feb 24 11:12:33 CET 2006


  Hi.


I have a question about DBIx::Class::Schema::Loader's (0.02003)
automatic has_many-releationship creation; more specifically the
naming of the relationsships.

It is probably easiest to illustrate with a tiny example. When I
create a database like this:

CREATE TABLE cars (
  id serial NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE passengers (
  id serial NOT NULL,
  car integer REFERENCES cars NOT NULL,
  PRIMARY KEY (id)
);

... what I expect to get is a Passengers->belongs_to(car) and a
Cars->has_many(passengers)-relationship.

But the has_many is called "passenger", singular, by
DBIx::Class::Schema::Loader.

When I run this tiny test:

 #!/usr/bin/perl

 use strict;
 use warnings;

 package MyTest::DB;
 use base qw(DBIx::Class::Schema::Loader);

 __PACKAGE__->load_from_connection(
                                   connect_info=> [
                                                   'dbi:Pg:dbname=test;host=localhost',
                                                   'xxxx',
                                                   'xxxx',
                                                  ],
                                   relationships=>1,
                                   debug=>1,
                                  );

... the output is:

 ### START DBIx::Class::Schema::Loader dump ###
 # Initializing table "cars" as "MyTest::DB::Cars"
 MyTest::DB::Cars->table('cars');
 MyTest::DB::Cars->add_columns('id')
 MyTest::DB::Cars->set_primary_key('id')
 # Loaded external class definition for 'MyTest::DB::Cars'
 # Initializing table "passengers" as "MyTest::DB::Passengers"
 MyTest::DB::Passengers->table('passengers');
 MyTest::DB::Passengers->add_columns('id', 'car')
 MyTest::DB::Passengers->set_primary_key('id')
 # Loaded external class definition for 'MyTest::DB::Passengers'
 # Belongs_to relationship
 MyTest::DB::Passengers->belongs_to( 'car' => 'MyTest::DB::Cars',{ id => car });

 # Has_many relationship
 MyTest::DB::Cars->has_many( 'passenger' => 'MyTest::DB::Passengers',{ foreign.car => self.id });

 );

 ### END DBIx::Class::Schema::Loader dump ###

(Notice the "has_many( 'passenger' =>"-part, I would have expected
"has_many( 'passengers' =>").

Am I naming my tables/fields wrong with respect to what
DBIx::Class:Schema::Loader expects, or is this behaviour a buglet?

If the latter is true should the patch below, or something similar, be
applied?


  Best regards,

    Adam


--- Generic.pm.orig	2006-02-24 10:13:15.000000000 +0100
+++ Generic.pm	2006-02-24 10:22:16.000000000 +0100
@@ -284,7 +284,7 @@
 
     my $table_class = $self->classes->{$table};
     my $other_class = $self->classes->{$other};
-    my $table_relname = $self->_inflect_relname(lc $table);
+    my $table_relname = lc $table;
     my $other_relname = lc $other;
 
     # for single-column case, set the relname to the column name,


-- 
 "Of all the great programmers I can think of, I know         Adam Sjøgren
  of only one who would voluntarily program in Java. And  asjo at koldfront.dk
  of all the great programmers I can think of who don't
  work for Sun, on Java, I know of zero."




More information about the Dbix-class mailing list