[Dbix-class] Inflation (DBIx::Class::InflateColumn) Troubles...

Jason Kohles email at jasonkohles.com
Sun May 6 00:11:33 GMT 2007


I'm trying to track down a problem I'm having that seems to be an  
issue with DBIx::Class::InflateColumn.  I'm using  
InflateColumn::DateTime and InflateColumn::Currency, as well as some  
hand-built inflate/deflate calls that all seem to have similar  
problems.  It appears that in some circumstances the values assigned  
to inflated columns will not be deflated before being passed to the  
database, and I'm having a heck of a time figuring out why that is.   
The minimal test case below uses InflateColumn::Currency simply  
because it's the easiest to see the problem there (since the inflated  
values will have dollar-signs that make it easy to see that they are  
still inflated)...

# lib/TestSchema.pm
package TestSchema;
use strict;
use warnings;
use base qw( DBIx::Class::Schema );

__PACKAGE__->load_classes();
__PACKAGE__->connection( 'DBI:mysql:test' );

# lib/TestSchema/Foo.pm
package TestSchema::Foo;
use strict;
use warnings;
use base qw( DBIx::Class );

__PACKAGE__->load_components(qw( InflateColumn::Currency Core ));
__PACKAGE__->table( 'foo' );
__PACKAGE__->add_columns(
     id => { data_type => 'integer', is_auto_increment => 1 },
     money => { data_type => 'float', is_currency => 1 },
);
__PACKAGE__->set_primary_key( qw( id ) );

1;

# test.t
use lib 'lib';
use TestSchema;
use Test::More tests => 4;

my $schema = TestSchema->connect;
$schema->deploy( { add_drop_table => 1 } );

my $obj = $schema->resultset( 'Foo' )->new( { money => 123 } );
is( $obj->money, '$123.00' );

$obj->insert;
$obj->discard_changes;
is( $obj->money, '$123.00' );

my $id = $obj->id;

$obj->update( { money => '$456.00' } );
$obj->discard_changes;
is( $obj->money, '$456.00' );

$obj->money( '$42.00' );
$obj->update;
$obj->discard_changes;

is( $obj->money, '$42.00' );


Running the test.t script produces:

1..4
ok 1
ok 2
not ok 3
#   Failed test at test.t line 19.
#          got: '$0.00'
#     expected: '$456.00'
not ok 4
#   Failed test at test.t line 25.
#          got: '$0.00'
#     expected: '$42.00'
# Looks like you failed 2 tests of 4.

With DBIC_TRACE=1 you can see that both of the updates fail because  
the values they provide to mysql are not valid floats.

UPDATE foo SET money = ? WHERE ( id = ? ): '$456.00', '1'
UPDATE foo SET money = ? WHERE ( id = ? ): '$42.00', '1'

At least part of my problem is a mysql issue, since mysql doesn't  
seem to report errors when you try and put something invalid into the  
money column (it just fills it with a 0 instead), but I'll tackle  
that next, for now I need to figure out why the values aren't being  
deflated before they get inserted...

-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Dbix-class mailing list