I have this migration:
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);
}
I am trying to set table with column created_at
with default value - current datetime.
I am using 'default' => 'CURRENT_TIMESTAMP'
, but I am getting this error:
Invalid default value for 'created_at' .... NOT NULL,
created_at
datetime NOT NULL DEFAULT 'CURRENT_TIMESTAMP',
I am using CodeIgniter 3
with MySQL
.
this worked for me, and is valid in the documentation
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',
),
)
);`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With