Question

What's wrong with this code? I am using perl's DateTime module with DBIx::Class.

my $entry_rs = $schema->resultset('table_name')->search_rs(             
    {                                                                       
        table_id      => $row->table_id,                                    
        utc_timestamp => { '<', \'DateTime->now()->subtract(days => 180)' },
    },                                                                      
)->all;                                                                     

I am using PostgreSQL, utc_timestamp type is timestamp. If I calculate, DateTime->now()->subtract(days => 180) and store in variable and substitute the value, then it works.

Error:

DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::Pg::st execute failed: ERROR: column "datetime" does not exist ??

Please help

Was it helpful?

Solution

You're passing Perl code as literal SQL to the database. This can't work. Try something like this:

my $dt       = DateTime->now()->subtract(days => 180);
my $parser   = $schema->storage->datetime_parser;
my $entry_rs = $schema->resultset('table_name')->search_rs(             
    {                                                                       
        table_id      => $row->table_id,                                    
        utc_timestamp => { '<', $parser->format_datetime($dt) },
    },                                                                      
)->all;

This code uses datetime_parser in DBIx::Class::Storage::DBI to retrieve the DateTime::Format class appropriate for your database. Then it calls format_datetime on that class to convert the DateTime object to a string that can be passed to the DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top