[Dbix-class] The Trouble With Inserts

Eric Wright info at rapidsynergy.com
Wed Jan 7 14:15:36 GMT 2009


I've been working with Catalyst and DBIx::Class quite a bit lately and think
that they are both awesome. However as my app has been growing in complexity
I've started to notice some troubling quirks with my DB inserts and selects.
I'm not sure if it's a Catalyst issue or a DBIC issue or an issue with my
code. I am using MySQL 5.0.27.

In a nutshell, what seems to be a recurring pattern is that when a record is
inserted with an auto incremented PK, that record *temporarily* shows up in
a select on that same table. For instance, if I insert a record into some
table and then do a find_or_new on that same table for an undefined primary
key I get the last record inserted. This behavior does not fix itself unless
I reboot Catalyst.

my $rec =3D $c->model("DB::MyTable")->new({ value =3D> $some_value });
$rec->insert;

Later on...

my $record =3D $c->model("DB::MyTable")->find_or_new({ id =3D> undef });
#Returns last inserted record!

The way around this was for me was to not use find_or_new but explicitly
build my record as needed. e.g.

my $record =3D (!$id)
    ? $c->model("DB::MyTable")->new({ id =3D> undef, value =3D> '' })
    : $c->model("DB::MyTable")->find({ id =3D> $id });

I'm not too keen on that but that works.

However, I have another situation where I might be pulling records based on
a belongs_to relationship.

__PACKAGE__->belongs_to(
  "mytable_id",
  "MyApp::Schema::MyTable",
  {
    "id" =3D> "mytable_id",
  },
);

And here that newly inserted record shows up in the belongs_to record but
only *once*. i.e. Repeated calls to retrieve that record return it to its
normal state. Weird right?

I suspected maybe it had something to do with the primary key but after
looking into it seems like PK::Auto should do the trick. Even though
PK::Auto is in core and my DBIC classes are generated via the Catalyst
scaffolding, just for the heck of it, I went and modified explicity to:

__PACKAGE__->load_components(qw/PK::Auto Core/);

But no difference.

I'm a little stumped. Does DBIx::Class have some kind of caching mechanism
that I don't understand? Am I overlooking an important step?

Thanks in advance for any insight into this perplexing problem!

-Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20090107/271=
2889c/attachment.htm


More information about the DBIx-Class mailing list