[Dbix-class] bad relation name

John Napiorkowski jjn1056 at yahoo.com
Fri Jan 19 19:43:54 GMT 2007


--- Octavian Rasnita <orasnita at gmail.com> wrote:

> Hi,
> 
> I have created the table class using the Catalyst
> helper DBIC::Schema but I 
> see that it automaticly chooses the same name for
> the relations as for the 
> table rows, so it doesn't work in some cases.
> If I will choose other names for the relations, I
> will need to do this 
> manually each time I will run the helper program to
> re-create the table 
> classes.
> 
> Isn't there a better way of naming the fields, or
> running the Catalyst 
> helper program, or doing something else that won't
> require renaming the 
> relations manually?

I don't love all the defaults either, you have some
ability to override them.  You can create a custom
subclass like:

package myapp::Schema::loaderdb;

use warnings;
use strict;

use base qw/DBIx::Class::Schema::Loader/;

my $dsn = 'dbi:Pg:dbname=postgres;host=localhost';
my $dbuser = 'xxx';
my $dbpass = 'xxx';

my $options = { 

   RaiseError => 1, 
   PrintError => 0, 
   ShowErrorStatement => 1, 
   TraceLevel => 0,
};
	
__PACKAGE__->loader_options(

   relationships => 1,
   db_schema => 'wikis',

   additional_base_classes =>
qw/talentspace::Schema::base/,

  moniker_map => sub { return $_[0] },
	
);

__PACKAGE__->connection( $dsn, $dbuser, $dbpass,
$options);


return 1;

and then run it from the command line with something
like:

perl
-MDBIx::Class::Schema::Loader=dump_to_dir:/mnt/hgfs/
-Mmyapp::Schema::loaderdb -e1

There's probably an easier way to do this, but this
works for me.

The key to making your subclass is the
"loader_options" method, which has all the things you
can change.  In my case I am specifying a specific
database schema, (since Postgres allows you to have
multiple schemas in a DB), I'm adding in some base
classes to the generated classes and I'm tweaking the
names of the relationship.

You'll need to read up on all the things you can put
into this.  I'm not sure if there is an option for
what you want.  See both:

http://search.cpan.org/~blblack/DBIx-Class-Schema-Loader-0.03009/lib/DBIx/Class/Schema/Loader.pm#loader_options

and

http://search.cpan.org/~blblack/DBIx-Class-Schema-Loader-0.03009/lib/DBIx/Class/Schema/Loader/Base.pm

for crazy details on everything the system can do
right now.  There are some settings there called
"inflect_plural" and "inflect_singular" which might
give you the control you want.

I find that even after this I need to tweak the output
a bit.   For example I find that it sets the
"data_type" of date fields to something like
"timestamp with timezone" which causes the auto
inflate and deflate time component to not realize this
field is a date field, so I end up removing that text
manually.  I keep meaning to make time to offer a
patch for this, but you know how it goes :)

Hope that helps you.

--john

> 
> Thank you.
> 
> Here is the table class:
> 
> package Intra::UserRole;
> 
> # Created by DBIx::Class::Schema::Loader v0.03009 @
> 2007-01-19 20:56:50
> 
> use strict;
> use warnings;
> 
> use base 'DBIx::Class';
> 
> __PACKAGE__->load_components("PK::Auto", "Core");
> __PACKAGE__->table("user_role");
> __PACKAGE__->add_columns(
>   "id_user",
>   { data_type => "INT", default_value => 0,
> is_nullable => 0, size => 10 },
>   "id_role",
>   { data_type => "INT", default_value => 0,
> is_nullable => 0, size => 10 },
> );
> __PACKAGE__->set_primary_key("id_user", "id_role");
> __PACKAGE__->belongs_to("id_user", "Intra::User", {
> id => "id_user" });
> __PACKAGE__->belongs_to("id_role", "Intra::Role", {
> id => "id_role" });
> 
> 1;
> 
> Octavian
> 
> 
> _______________________________________________
> List:
>
http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN:
>
http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> Searchable Archive:
>
http://www.mail-archive.com/dbix-class@lists.rawmode.org/
> 



 
____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check. 
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html 



More information about the Dbix-class mailing list