<?php defined("BASEPATH") or exit("No direct script access allowed");
class Migration_Create_clients_categories_table extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE,
),
'parent_id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'default' => NULL,
'null' => TRUE,
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('clients_categories');
}
public function down()
{
$this->dbforge->drop_table('clients_categories');
}
}在运行迁移之后,我得到了以下错误:
错误号: 1067,“parent_id”
parent\_idINT(11)无符号默认值“NULL”的默认值无效
问题是,我不知道dbforge为什么会生成\缺省值'‘NULL\,因为它应该只是\默认NULL。有什么想法吗?
发布于 2018-06-25 13:24:58
它看起来像是对CI数据库驱动程序中的_process_fields()函数的限制。上面写着:
if (array_key_exists('DEFAULT', $attributes))
{
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}...so,这些引号被硬编码到MySQL调用中。
但是(对我最初回答的内容进行彻底的编辑):如果一个字段是NULLable,并且没有设置默认值,那么如果您理解我的意思,MySQL将在默认情况下使默认值为空。
从手册中:
如果列定义不包含显式默认值,MySQL将默认值确定如下: 如果列可以将NULL作为值,则使用显式默认NULL子句定义该列。 ..。
因此,使用迁移或dbForge无法做到这一点基本上是很好的。
https://stackoverflow.com/questions/44797671
复制相似问题