【问题标题】:Set default (current datetime) to table column - CodeIgniter 3 migration将默认(当前日期时间)设置为表列 - CodeIgniter 3 迁移
【发布时间】:2018-07-25 02:32:06
【问题描述】:

我有这个迁移:

public function up(){
    $this->dbforge->add_field([
        'id'=>['type'=>'int', 'unique'=>true, 'unsigned'=>true,'auto_increment'=>true],
        'email'=>['type'=>'varchar', 'constraint'=>200, 'null'=>true],
        'password'=>['type'=>'varchar', 'constraint'=>250],
        'created_at'=>['type'=>'datetime', 'default' => 'CURRENT_TIMESTAMP'],
    ]);
    $this->dbforge->add_key('id', TRUE);
    $this->dbforge->create_table('users', TRUE);
}

我正在尝试使用默认值 - 当前日期时间设置包含 created_at 列的表。 我正在使用'default' => 'CURRENT_TIMESTAMP',但出现此错误:

'created_at' 的默认值无效 .... NOT NULL, created_at datetime NOT NULL DEFAULT 'CURRENT_TIMESTAMP',

我正在使用CodeIgniter 3MySQL

【问题讨论】:

  • 而不是'created_at'=>['type'=>'datetime', 'default' => 'CURRENT_TIMESTAMP'] 试试'created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP' -
  • 嗨,谢谢。它必须放在列名键中,而不是 created_at 将是 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP 并删除此键中的数组(带有类型和默认值)。

标签: codeigniter codeigniter-3


【解决方案1】:

这对我有用,并且在文档中有效

将字符串作为字段传递 如果您确切知道要如何使用字段 被创建,您可以将字符串传递到字段定义中 add_field()

$this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT '默认标签'");

注意: 将原始字符串作为字段传递不能跟随 add_key() 调用 那些字段。

`$this->dbforge->add_field(
  array(
    'nombre' => 
      array(
        'type' => 'VARCHAR',
        'constraint' => '150',
      ),
    'paterno' => 
      array(
        'type' => 'VARCHAR',
        'constraint' => '80',
      ),
    'materno' => 
      array(
        'type' => 'VARCHAR',
        'constraint' => '80',
      ),
    'correo' => 
      array(
        'type' => 'varchar',
        'constraint' => '150',
        'unique'=> TRUE,
    ),
    'username' => 
      array(
        'type' => 'varchar',
        'constraint' => '80',
    ),
    'genero' => 
      array(
        'type' => 'varchar',
        'constraint' => '30',
    ),
    'pass' => 
      array(
        'type' => 'varchar',
        'constraint' => '255',
    ),
    'direccion' => 
      array(
        'type' => 'varchar',
        'constraint' => '200',
    ),
    'telefono' => 
      array(
        'type' => 'varchar',
        'constraint' => '30',
    ),
    'celular' => 
      array(
        'type' => 'varchar',
        'constraint' => '30',
    ),
    'created_at datetime default current_timestamp',
    'updated_at datetime default current_timestamp on update current_timestamp',
    'status' => 
      array(
        'type' => 'tinyint',
        'constraint' => '1',
    ),
  )
);`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2013-08-06
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多