[Dbix-class] update_or_create(\%hash) deletes keys from %hash when
updating an inflated column
Ronald J Kimball
rkimball+dbixclass at pangeamedia.com
Wed Apr 14 16:25:28 GMT 2010
I'm passing a reference to a named hash to update_or_create(). The
resultset has two inflated columns. If the row already exists in the
table, then the keys for the inflated columns are deleted from the hash.
Is this expected behavior? This happens with DBIC 0.08111 and 0.08121.
For example, with this schema:
package MyApp::Schema::MyDB::User;
use strict;
use warnings;
use JSON::XS;
use base 'DBIx::Class';
__PACKAGE__->load_components("Core");
__PACKAGE__->table("user");
__PACKAGE__->add_columns(
"user_id",
{ data_type => "INT", default_value => undef,
is_nullable => 0, size => 11 },
"original_pixel_id",
{ data_type => "TEXT", default_value => undef,
is_nullable => 0, size => 65535 },
);
__PACKAGE__->set_primary_key("user_id");
__PACKAGE__->inflate_column('user_data', {
inflate => sub {
my $val = shift;
defined $val && length $val
or return $val;
return decode_json($val);
},
deflate => sub {
my $val = shift;
return encode($val);
},
});
running this code:
use Data::Dump qw/ dump /;
$model->resultset('User')->search({ user_id => 1 })->delete;
my %user = (
user_id => 1,
user_data => { first_name => 'John', last_name => 'Smith' },
);
$model->resultset('User')->update_or_create(\%user);
print dump(\%user), "\n";
$model->resultset('User')->update_or_create(\%user);
print dump(\%user), "\n";
results in the following output:
{
user_data => { first_name => 'John', last_name => 'Smith' },
user_id => 1,
}
{
user_id => 1,
}
After the update, the user_data key has been deleted from the hash.
This was very surprising to me when I ran my script the second time,
with the table already populated, and got weird results.
Ronald
More information about the DBIx-Class
mailing list