DBIx :: Class :: Schema :: Loader defining CURRENT_TIMESTAMP as a string, not a string reference
I have a script that uses Class :: DBIx :: Schema-> deploy to create a database for an application I am creating.
I am using mysql 5.6.19
Some 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, the deployment puts quotes around the CURRENT_TIMESTAMP like this:
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
Quotes cause error "Invalid default value for" created ", removing quotes and running this command from command line is fine, if quotes stay in place it fails.
This is because in Schema :: Result we have the string "CURRENT_TIMESTAMP" and not the string reference "CURRENT_TIMESTAMP"
Schema :: Result :: CompanyInfo looks like this:
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 the default => "CURRENT_TIMESTAMP", default_value => "CURRENT_TIMESTAMP" fixes the problem, however, since this is generated by DBIx :: Class :: Schema :: Loader, I would not manually edit all associated files.
Is this a bug or is there a way to tell DBIx :: Class :: Schema :: Loader to reference the string around CURRENT_TIMESTAMP?
thank
source to share
In c: \ Perl \ site \ lib \ DBIx \ Class \ Schema \ Loader \ DBI \ mysql.pm (l. 305) there is a condition that replaces the string with a link. For some reason this only applies to "timestamp", but you can make changes to "datetime" too.
if ((not blessed $dbi_info) # isa $sth
&& lc($dbi_info->{COLUMN_DEF}) eq 'current_timestamp'
&& lc($dbi_info->{mysql_type_name}) eq 'timestamp') {
my $current_timestamp = 'current_timestamp';
$extra_info{default_value} = \$current_timestamp;
}
source to share