Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

DBIx::Class 'select.... for update' confusion

by tomgracey (Scribe)
on Dec 14, 2017 at 21:17 UTC ( [id://1205549]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all

I am confused about the mechanics of 'select.. for update' under DBIx::Class (I'm using MySQL but I suppose that shouldn't matter.) So I have reduced it down to the following test case:

my $guard = $schema->txn_scope_guard; my $rs = $schema->resultset('TestTable')->search({ status => 'available' }, { rows => 1, 'for' => 'update' }); my $id_before_update = $rs->first->id; my $updated = $rs->update({ status => 'reserved' }); $guard->commit; my $id_at_end = $rs->first->id; print "id before update: $id_before_update\nid at end: $id_at_end\n";
The table looks like this initially:
+----+-----------+ | id | status | +----+-----------+ | 1 | available | | 2 | available | | 3 | available | +----+-----------+

and after execution like this:

+----+-----------+ | id | status | +----+-----------+ | 1 | reserved | | 2 | available | | 3 | available | +----+-----------+

The schema was created by DBIx::Class::Schema::Loader and looks like this:

__PACKAGE__->table("test_table"); __PACKAGE__->add_columns( "id", { data_type => "bigint", is_nullable => 0 }, "status", { data_type => "varchar", is_nullable => 1, size => 255 }, );
(with boilerplate removed). When I run the above code I get the following:
id before update: 1 id at end: 2

To me this seems like odd behaviour, because after $rs is selected with rows=1 it surely should only contain a single row with a single id...?

Any idea what stupid thing have I (not) done this time?

Replies are listed 'Best First'.
Re: DBIx::Class 'select.... for update' confusion
by poj (Abbot) on Dec 14, 2017 at 21:43 UTC

    From the docs it looks like ->first resets the resultset (causing a fresh query to storage).

    poj

      And more importantly, you didn't specify an order by clause, so the exact order of your search results will change over time.

        Actually in this particular case the order doesn't matter... but thanks for pointing it out!

      Ah yes, silly me. Thanks...!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205549]
Approved by marto
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-26 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found