繁体   English   中英

Codeigniter 2.2.2 dbforge产生DEFAULT''NULL

[英]Codeigniter 2.2.2 dbforge produces DEFAULT '' NULL

<?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_id`无效的默认值INT(11)UNSIGNED DEFAULT''NULL

问题是我不知道为什么dbforge产生\\ DEFAULT''NULL \\,因为它应该只是\\ DEFAULT NULL。 任何想法?

看起来对CI数据库驱动程序中_process_fields()函数的限制。 它说:

if (array_key_exists('DEFAULT', $attributes))
{
    $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\'';
}

...因此这些引号被硬编码到MySQL调用中。

但是(对我最初回答的内容进行激进编辑):如果字段是可空的并且没有设置默认值,那么如果您明白我的意思,MySQL将默认使用默认NULL。

从手册中:

如果列定义不包含任何显式的DEFAULT值,则MySQL按照以下方式确定默认值:

如果该列可以采用NULL作为值,则使用显式的DEFAULT NULL子句定义该列。

...

因此,使用迁移或dbForge无法做到这一点基本上是可以的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM