<?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,&#39; parent_id&#39;的默认值无效 `parent_id` INT(11)UNSIGNED DEFAULT&#39;&#39; NULL
问题是我不知道为什么dbforge会产生\
DEFAULT&#39;&#39; NULL \因为它应该只是\
DEFAULT NULL。有什么想法吗?
答案 0 :(得分:0)
看起来像CI数据库驱动程序中_process_fields()函数的限制。它说:
if (array_key_exists('DEFAULT', $attributes))
{
$sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}
...因此这些引号被硬编码到MySQL调用中。
但是(对我最初回答的内容进行彻底编辑):如果一个字段是可空的并且没有设置默认值,那么如果您明白我的意思,MySQL将默认使用默认的NULL。
从手册:
如果列定义中不包含任何显式的DEFAULT值,则MySQL按照以下方式确定默认值:
如果列可以采用NULL作为值,则使用显式的DEFAULT NULL子句定义该列。
...
因此,使用迁移或dbForge无法做到这一点基本上是可以的。