mark mark - 5 months ago 11
Perl Question

DBIx::Class::Schema::Loader defining CURRENT_TIMESTAMP as a string rather than string reference

I have a script that uses Class::DBIx::Schema->deploy to create a database for an application I'm building.

I'm using mysql 5.6.19

Some of the tables have datetime fields that have a default value of CURRENT_TIMESTAMP (which is valid for mysql versions > 5.6)

When I run the code to populate the DB, deploy is putting quotes around CURRENT_TIMESTAMP as follows:

CREATE TABLE `company_info` (
`id` bigint unsigned NOT NULL auto_increment,
`ugroup` bigint unsigned NULL,
`created` datetime NULL DEFAULT 'CURRENT_TIMESTAMP',
`num_employees` integer NOT NULL DEFAULT 1,
`type` char(16) NULL,
INDEX `company_info_idx_ugroup` (`ugroup`),
PRIMARY KEY (`id`),
CONSTRAINT `company_info_fk_ugroup` FOREIGN KEY (`ugroup`) REFERENCES `groups` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB


The quotes cause an error "Invalid default value for 'created'", removing the quotes and running this command from the command line works fine, if the quotes are left in place, it fails.

This is because in the Schema::Result, we have a string "CURRENT_TIMESTAMP" rather then a string reference \"CURRENT_TIMESTAMP"

Schema::Result::CompanyInfo looks as follows and is generated;

use utf8;
package Schema::Result::CompanyInfo;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';
__PACKAGE__->load_components("InflateColumn::DateTime");
__PACKAGE__->table("company_info");
__PACKAGE__->add_columns(
"id",
{
data_type => "bigint",
extra => { unsigned => 1 },
is_auto_increment => 1,
is_nullable => 0,
},
"ugroup",
{
data_type => "bigint",
extra => { unsigned => 1 },
is_foreign_key => 1,
is_nullable => 1,
},
"created",
{
data_type => "datetime",
datetime_undef_if_invalid => 1,
default_value => "CURRENT_TIMESTAMP",
is_nullable => 1,
},
"num_employees",
{ data_type => "integer", default_value => 1, is_nullable => 0 },
"type",
{ data_type => "char", is_nullable => 1, size => 16 },
);
__PACKAGE__->set_primary_key("id");
__PACKAGE__->belongs_to(
"ugroup",
"Schema::Result::Group",
{ id => "ugroup" },
{
is_deferrable => 1,
join_type => "LEFT",
on_delete => "SET NULL",
on_update => "CASCADE",
},
);


# Created by DBIx::Class::Schema::Loader v0.07042 @ 2014-11-24 14:30:12
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Q6PHwuB2Zk74lVC08u8CMQ


# You can replace this text with custom code or comments, and it will be preserved on regeneration
1;


Changing default_value => "CURRENT_TIMESTAMP", to default_value => \"CURRENT_TIMESTAMP",
fixes the problem, however since this is generated by DBIx::Class::Schema::Loader I'd rather not manually edit all the related files.

Is this a bug or is there a way to tell DBIx::Class::Schema::Loader to create a string reference around CURRENT_TIMESTAMP?

Thanks

Answer

I believe the argument to default_value should be a string reference:

default_value => \"CURRENT_TIMESTAMP",
Comments