DBIx :: Class :: Schema :: Loader将CURRENT_TIMESTAMP定义为字符串而不是字符串引用

时间:2014-12-12 12:54:03

标签: perl dbix-class

我有一个脚本,使用Class :: DBIx :: Schema-> deploy为我正在构建的应用程序创建数据库。

我正在使用mysql 5.6.19

某些表的日期时间字段的默认值为CURRENT_TIMESTAMP(对于mysql版本> 5.6有效)

当我运行代码来填充数据库时,deploy会在CURRENT_TIMESTAMP周围加上引号,如下所示:

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

引号导致错误"'创建'"无效的默认值,删除引号并从命令行运行此命, 它失败。

这是因为在Schema :: Result中,我们有一个字符串" CURRENT_TIMESTAMP"而不是字符串引用\" CURRENT_TIMESTAMP"

Schema :: Result :: CompanyInfo如下所示并生成;

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;

更改default_value => " CURRENT_TIMESTAMP",to default_value => \" CURRENT_TIMESTAMP&#34 ;, 修复问题,但是因为这是由DBIx :: Class :: Schema :: Loader生成的,而不是手动编辑所有相关文件。

这是一个错误还是有办法告诉DBIx :: Class :: Schema :: Loader在CURRENT_TIMESTAMP周围创建一个字符串引用?

由于

2 个答案:

答案 0 :(得分:3)

我相信default_value的参数应该是一个字符串引用:

default_value => \"CURRENT_TIMESTAMP",

答案 1 :(得分:1)

c:\ Perl \ site \ lib \ DBIx \ Class \ Schema \ Loader \ DBI \ mysql.pm(l.305)中有一个条件,它用引号替换字符串。出于某种原因,它仅用于'时间戳',但您也可以进行修改以处理'datetime'。

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;
}