【问题标题】:Invalid default value for 'record_date' timestamp“record_date”时间戳的默认值无效
【发布时间】:2017-05-22 11:41:49
【问题描述】:

我在 drupal7 中创建了 Schema

 $schema['survey'] = array(
      'description' => 'TODO: please describe this table!',
      'fields' => array(
        'id' => array(
          'description' => 'TODO: please describe this field!',
          'type' => 'serial',
          'not null' => TRUE,
        ),
        'survey_id' => array(
          'description' => 'TODO: please describe this field!',
          'type' => 'int',
          'not null' => FALSE,
....
 'record_date' => array(
          'description' => 'TODO: please describe this field!',
          'mysql_type' => 'timestamp',
          'not null' => FALSE,
        ),
        'facebookid' => array(
          'description' => 'TODO: please describe this field!',
          'type' => 'varchar',
          'length' => '10',
          'not null' => FALSE,
)

/**
 * hook_enable()
 */
function itg_be_lucky_today_enable() {
  db_query('
    ALTER TABLE {survey} 
    MODIFY record_date TIMESTAMP NOT NULL 
    DEFAULT CURRENT_TIMESTAMP 
    ON UPDATE CURRENT_TIMESTAMP'
  );
}

当我尝试启用模块时显示错误:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'record_date': CREATE TABLE {survey} ( `id` INT NOT NULL auto_increment COMMENT 'TODO: please describe this field!', `survey_id` INT NULL DEFAULT NULL COMMENT 'TODO: please describe this field!', `name` VARCHAR(100) NULL

我已经搜索过,但没有找到适合我的解决方案。

这在我的本地机器上运行良好。 我也在mysql版本上测试过:5.7和5.5

谢谢

【问题讨论】:

    标签: mysql drupal drupal-7 drupal-modules


    【解决方案1】:

    试试这个:

    'record_date' => array(
      'description' => 'TODO: please describe this field!',
      'type' => 'datetime',
      'mysql_type' => 'datetime',
      'not null' => FALSE,
    ),
    

    更详细的答案见this post

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 1970-01-01
      • 2018-10-13
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多