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

Hi Monks,

I am trying to use DBIx::Connector for my DBIx Schema object MyTest::Schema as shown below:

use MyTest::Schema; use DBIx::Connector; my $args = [ 'DBI:mysql:my_db:192.XXX.XXX.25', 'XX', 'XX', ]; my $schema = MyTest::Schema->connect( sub { DBIx::Connector->new( @{$args} ); } );

I am getting below error as a result:

DBIx::Class::Storage::DBI::catch {...} (): DBI Connection failed: Can't locate object method "FETCH" via package "DBIx::Connector" at /usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1494.

 at /usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1494.
	DBIx::Class::Storage::DBI::try {...} () called at /usr/local/share/perl/5.18.2/Try/Tiny.pm line 81
	eval {...} called at /usr/local/share/perl/5.18.2/Try/Tiny.pm line 72
	Try::Tiny::try(CODE(0xdec1bb8), Try::Tiny::Catch=REF(0xbafa428)) called at /usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1531
	DBIx::Class::Storage::DBI::_connect(DBIx::Class::Storage::DBI=HASH(0xdc024e8)) called at /usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1048
	DBIx::Class::Storage::DBI::_populate_dbh(DBIx::Class::Storage::DBI=HASH(0xdc024e8)) called at /usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1317
	DBIx::Class::Storage::DBI::_extract_driver_from_connect_info(DBIx::Class::Storage::DBI=HASH(0xdc024e8)) called at /usr/local/share/perl/5.18.2/DBIx/Class/Storage/DBI.pm line 1261
	DBIx::Class::Storage::DBI::_determine_driver(DBIx::Class::Storage::DBI=HASH(0xdc024e8)) called at (eval 1029) line 37
	DBIx::Class::Storage::DBI::select_single(DBIx::Class::Storage::DBI=HASH(0xdc024e8), ARRAY(0x3089fa8), ARRAY(0xbc35a40), ARRAY(0xbb5da58), HASH(0xdf0f9c8)) called at /usr/local/share/perl/5.18.2/DBIx/Class/ResultSet.pm line 1099

What could I be doing wrong - wise monks.
  • Comment on DBI Connection failed: Can't locate object method "FETCH" via package "DBIx::Connector"
  • Select or Download Code

Replies are listed 'Best First'.
Re: DBI Connection failed: Can't locate object method "FETCH" via package "DBIx::Connector"
by Myrddin Wyllt (Hermit) on Aug 07, 2015 at 13:37 UTC

    You seem to be mixing up DBIx::Class::Schema and DBIx::Connector. Although they are both mechanisms for talking to databases in an Object-Oriented way, you wouldn't typically use them together like that.

    The DBIx::Schema 'connect' method just takes a DBI connect list, whereas you are feeding it a DBIx::Connector object, which is why it is complaining. You just need to change the connect bit to :

    my $schema = MyTest::Schema->connect( 'DBI:mysql:my_db:192.XXX.XXX.25', 'XX', 'XX' );

    Update: It seems connect will also take a code-ref returning a DBI database handle (I thought that you could only do that in connection, you live and learn). The problem is still that you are giving it a DBIx::Connector object rather than a database handle, which can be fixed thusly:

    my $schema = MyTest::Schema->connect( sub { my $dbh = DBIx::Connector->new( @{$args} )->dbh; $dbh->{Active} = 1; return $dbh; }

    The setting of 'Active' to 'true' seems to be necessary with PostgreSQL (not sure why), but couldn't test against MySQL, so you may not require this. You may have a particular reason for using DBIx::Connector elsewhere in your code, but I would point out what the documentation says about creating a Connector object just to return a handle:

    "..there's probably not much point in that, as you'll generally want to hold on to the DBIx::Connector object. Otherwise you'd just use the DBI, no?"