Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

DBIx::Class "Can't call method 'select' on an undefined value"

by sundialsvc4 (Abbot)
on Jan 21, 2009 at 23:44 UTC ( #737992=perlquestion: print w/replies, xml ) Need Help??

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

Okay, let me start over on this post... :-)

The code looks like this:

my $schema = My::Schema::Class->connect ("dbi:mysql:dbname=BLAHBLAH;host=localhost", "BLAH", "HEAPBIGSECRET", {AutoCommit => 1}) or croak("cannot connect to main database"); # Strobe the database-connection right now to be sure it's here $schema->storage->ensure_connected(); croak("connection failed to main database") unless $schema->storage->connected;

So far so good... but now, in my test-script...
lives_ok { require My::Schema::Class::TFoodtype} "required OK"; lives_ok { My::Schema::Class->load_classes(qw/TFoodtype/) } "loaded one again"; isa_ok($s->schema->resultset('TFoodtype'), "DBIx::Class::ResultSet");

And this is what fails:
my @records; lives_ok { @records = $s->schema ->resultset('TFoodtype') ->search({foodtype=>'YUM'})->all() } "searched okay";

This is what it says:

Can't call method "select" on an undefined value at (..blah..)/5.8.8/DBIx/Class/ line 514.

Now, if you study the test-script closely (it uses Test::More and Test::Except ...), you will see that I am trying to systematically check everything I can think of. I know, by this point, that the connection is present. I know that the class is loaded. I know that the resultset('TFoodtype') call is returning a result-set. So it comes down to the final call... and I know that I am doing something abjectly stupid here and I just can't see it!

(Whimper...) My hand is poised, ready to slap my forehead...

Replies are listed 'Best First'.
Re: DBIx::Class "Can't call method 'select' on an undefined value"
by Your Mother (Archbishop) on Jan 22, 2009 at 01:06 UTC

    This is mostly guesswork so take it with a grain of salt but it looks like your test is not where your error is. That select error is related to calling ->cursor. Are you using DBIC cursors in there somewhere...? Your storage is apparently getting undefined/lost somewhere in any case. You could insert a test for that and move it back until you find the trigger.

Re: DBIx::Class "Can't call method 'select' on an undefined value"
by sundialsvc4 (Abbot) on Jan 22, 2009 at 01:07 UTC

    Looking further, the test:

    is($rs->result_source->schema, $s->schema, "belongs to the same schema");

    Fails with the message:

    # Failed test 'belongs to the same schema' # at t/0200_db/0200_dbconnect.t line 52. # got: 'My::Schema::Class' # expected: 'My::Schema::Class=HASH(0x8a163c4)'

    It appears to be saying that one is a string and the other is a hash...

    And indeed, $rs->result_source->storage is undefined.

Re: DBIx::Class "Can't call method 'select' on an undefined value"
by sundialsvc4 (Abbot) on Jan 22, 2009 at 01:57 UTC

    And doing load_classes in the schema base-file “magically” fixes it.

    Not good enough for me... I wanna understand this.

      load_classes() ensures everything is hooked together properly. It's a bit of side-effect magic that mst isn't thrilled with, but at least it's (mostly) all in one place. Go talk in if you really want to understand more.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: DBIx::Class "Can't call method 'select' on an undefined value"
by jasonk (Parson) on Jan 22, 2009 at 02:31 UTC

    What is $s? I would suspect that $s->schema is returning something other than the $schema object, like maybe the schema class name?
    We're not surrounded, we're in a target-rich environment!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://737992]
Approved by almut
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2022-05-26 20:11 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.