【问题标题】:How I Creat TIMESTAMP field while migration in CODEIGNITER with CURRENT_TIME on update如何在 CODEIGNITER 中迁移时创建 TIMESTAMP 字段,并在更新时使用 CURRENT_TIME
【发布时间】:2017-04-01 23:38:09
【问题描述】:

下面是我的代码,请告诉我我哪里错了..

public function up(){

        $this->dbforge->add_field(array(
                'id'=> array(
                    'type'          => 'int',
                    'constraint'    => 11,
                    'auto_increment'=> TRUE,
                ),
                'ip_address'=> array(
                    'type'      => 'VARCHAR',
                    'constraint'=> '40',
                ),
                'login'=> array(
                    'type'      => 'VARCHAR',
                    'constraint'=> '50',
                ),
                'time'=> array(
                    'type'      => 'timestamp',
                    'Default' => 'CURRENT_TIMESTAMP',
                    'ON UPDATE CURRENT_TIMESTAMP' => TRUE,
                ),
        ));
        $this->dbforge->add_key('id',TRUE);
        $this->dbforge->create_table('login_attempts');
    echo "table created";       
    }

这个错误会在运行迁移时产生

错误号:

1067

“时间”的默认值无效

CREATE TABLE login_attempts (id int(11) NOT NULL AUTO_INCREMENT, ip_address VARCHAR(40) NOT NULL,登录 VARCHAR(50) NOT NULL,时间 时间戳非空默认值'CURRENT_TIMESTAMP',约束 pk_login_attempts PRIMARY KEY(id) ) 默认字符集 = utf8 整理 = utf8_general_ci

文件名:D:/xampp/htdocs/tank_auth/system/database/DB_driver.php

行号:691

【问题讨论】:

    标签: codeigniter database-migration


    【解决方案1】:

    查看time 列的默认值。在 MySQL 中,CURRENT_TIMESTAMP 不能放在单括号中。由于 CodeIgniter 出于安全原因会自动转义默认值,因此您也可以传递一个完整的字符串,例如:

    'time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
    

    将其他列保留为数组。仅如上所示更改时间列。

    【讨论】:

      猜你喜欢
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2019-04-16
      • 2022-01-02
      • 2019-03-27
      相关资源
      最近更新 更多