gpt4 book ai didi

codeigniter - 将默认值(当前日期时间)设置为表列 - CodeIgniter 3 迁移

转载 作者:行者123 更新时间:2023-12-04 12:24:24 25 4
gpt4 key购买 nike

我有这个迁移:

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' ,但我收到此错误:

Invalid default value for 'created_at' .... NOT NULL, created_at datetime NOT NULL DEFAULT 'CURRENT_TIMESTAMP',



我正在使用 CodeIgniter 3MySQL .

最佳答案

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

Passing strings as fields If you know exactly how you want a field to be created, you can pass the string into the field definitions with add_field()

$this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");

Note: Passing raw strings as fields cannot be followed by add_key() calls on those fields.


`$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',
),
)
);`

关于codeigniter - 将默认值(当前日期时间)设置为表列 - CodeIgniter 3 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48788533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com