Korjavin Ivan
Korjavin Ivan

Reputation: 449

DBIx::class find function returns hash when value expected

Database schema:

create table requests(
    rid integer primary key autoincrement,
    oid integer references orders (oid),
    command varchar(5),
    account varchar(50),
    txn_id varchar(12),
    md5 varchar(30),
    txn_date varchar(14),
    sum float,
    regdt timestamp default current_timestamp,
    constraint u1 unique (command,txn_id)
);
create table orders (
    oid integer primary key autoincrement,
    cid integer references customers (cid),
    pid integer references providers (pid),
    account varchar(50),
    amount float
);  

Mapped to code by DBIx::Class::Schema::Loader.

My controller code :

 my $req= $schema->resultset('Request');
 my $order= $schema->resultset('Order');

      my $r= $req->find(
            {   command => 'check',
                txn_id  => $txn_id,
            },
            { key => 'command_txn_id_unique' }
        );
        my $oid=$r->oid;

        $req->create(
            {   command  => $command,
                account  => $account,
                txn_id   => $txn_id,
                md5      => $md5,
                txn_date => $txn_date,
                sum      => $sum,
                oid      => $oid
            }
        );
       my $o = $order->find($oid);

       $o->sum($sum);
       $o->update;

My tracert sqls with DBIC_TRACE=1

  SELECT me.rid, me.oid, me.command, me.account, me.txn_id, me.md5, me.txn_date, me.sum, me.regdt FROM requests me 
WHERE ( ( me.command = ? AND me.txn_id = ? ) ): 'check', '1358505665'
    SELECT me.oid, me.cid, me.pid, me.account, me.amount FROM orders me 
WHERE ( me.oid = ? ): '18'
    INSERT INTO requests ( account, command, md5, oid, sum, txn_date, txn_id) VALUES ( ?, ?, ?, ?, ?, ?, ? ): '1', 'pay', '44F4BC73D17E3FA906F658BB5916B7DC', '18', '500', '20130118104122', '1358505665'
    DBIx::Class::Storage::DBI::SQLite::_dbi_attrs_for_bind(): Non-integer value supplied for column 'me.oid' despite the integer datatype at /home/.../lib/TestPrj/Test.pm line 128
    SELECT me.oid, me.cid, me.pid, me.account, me.amount FROM orders me WHERE ( me.oid = ? ): 'TestPrj::Model::Result::Order=HASH(0x3dea448)'
    datatype mismatch: bind param (0) =HASH(0x3dea448) as integer at /usr/share/perl5/DBIx/Class/Storage/DBI.pm line 1765.

I don't understand :

Why in first select query $oid = 18 and its ok. Its really 18.

But in second select query this $oid is a HASH ? Its not redefined anywhere in my code.

UPD:

Using Data::Dumper by advice @akawhy I saw that $oid is a blessed HASH.

So, looks like there is different context

scalar in { ... oid => $oid .. .}

and hash in find(...).

I don't know why it not a hash in first case.

But, when I changed to $oid=$r->get_column($oid) all works fine.

Upvotes: 0

Views: 381

Answers (2)

Alexander Hartmaier
Alexander Hartmaier

Reputation: 2204

$resultset->find returns a Result object. You didn't paste your ResultSource classes but as you wrote that you generated them with Schema::Loader I assume the 'oid' column accessor method is the same as the 'oid' relationship accessor. In that case it returns different things depending on scalar vs. list context.

I suggest to rename your relationship so you have two different accessors for the column value and its relationship.

I prefix all relationships with 'rel_' but you might prefer a different naming standard.

Upvotes: 2

akawhy
akawhy

Reputation: 1608

I think my $oid=$r->oid; the $oid is a ref. you can use ref or Data::Dumper to see its type.

Maybe you should use $oid->oid

And pay attention to this error

DBIx::Class::Storage::DBI::SQLite::_dbi_attrs_for_bind(): Non-......

Upvotes: 1

Related Questions