在CodeIgniter迁移中为列添加注释

4

我正在为我的应用程序编写大量数据库迁移脚本。我希望为列添加注释,以便其他人可以轻松识别列的内容。一种选择是编写普通的SQL查询并添加注释。但是我是否可以在迁移脚本中添加这些注释呢?

$this->dbforge->add_field(array(
  'post_id' => array(
    'type' => 'INT',
    'constraint' => 11,
    'unsigned' => true,
    'auto_increment' => true,
    'comment' => 'Unique post id'
  ),
  'user_id' => array(
    'type' => 'INT',
    'constraint' => 11,
    'unsigned' => true,
  ),
  'group_id' => array(
    'type' => 'INT',
    'constraint' => 11,
    'unsigned' => true,
  ),
  'source' => array(
    'type' => 'VARCHAR',
    'constraint' => 20
  ),
  'data' => array(
    'type' => 'TEXT',
  ),
  'created' => array(
    'type' => 'INT',
    'constraint' => 11,
    'unsigned' => true,
  ),
  'updated' => array(
    'type' => 'INT',
    'constraint' => 11,
    'unsigned' => true,
  ),
  'status' => array(
    'type' => 'INT',
    'constraint' => 1,
    'unsigned' => true,
  )
));

这是我写的基本代码。可能有一些语法错误。但我只是复制粘贴它。

有人能帮忙吗?


你尝试过它,发生了什么事? - jmadsen
代码运行良好。但是我无法像通过phpmyadmin界面一样为列添加注释。 - Amitav Roy
各位大佬请帮忙。虽然这并不影响我的开发,但我想将这个功能放在那里。 - Amitav Roy
我已经尝试过使用“comment”/“comments”,虽然它不会导致错误,但也没有任何作用。难道没有其他人遇到这个问题吗? - JamesNZ
3个回答

7

CodeIgniter在3.0版本中增加了此功能。您可以使用“comment”键添加注释:

'first_name' => [
    'type'       => 'VARCHAR',
    'constraint' => 45,
    'null'       => false,
    'comment' => 'Put the field comment here',
]

1

查看CodeIgniter核心,特别是system/database/drivers/mysql/mysql_forge.php,看起来不支持COMMENT字段。

供参考,这是解析字段数组的函数:

function _process_fields($fields)
{
    $current_field_count = 0;
    $sql = '';

    foreach ($fields as $field=>$attributes)
    {
        // Numeric field names aren't allowed in databases, so if the key is
        // numeric, we know it was assigned by PHP and the developer manually
        // entered the field information, so we'll simply add it to the list
        if (is_numeric($field))
        {
            $sql .= "\n\t$attributes";
        }
        else
        {
            $attributes = array_change_key_case($attributes, CASE_UPPER);

            $sql .= "\n\t".$this->db->_protect_identifiers($field);

            if (array_key_exists('NAME', $attributes))
            {
                $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' ';
            }

            if (array_key_exists('TYPE', $attributes))
            {
                $sql .=  ' '.$attributes['TYPE'];

                if (array_key_exists('CONSTRAINT', $attributes))
                {
                    switch ($attributes['TYPE'])
                    {
                        case 'decimal':
                        case 'float':
                        case 'numeric':
                            $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
                        break;

                        case 'enum':
                        case 'set':
                            $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
                        break;

                        default:
                            $sql .= '('.$attributes['CONSTRAINT'].')';
                    }
                }
            }

            if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE)
            {
                $sql .= ' UNSIGNED';
            }

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

            if (array_key_exists('NULL', $attributes) && $attributes['NULL'] === TRUE)
            {
                $sql .= ' NULL';
            }
            else
            {
                $sql .= ' NOT NULL';
            }

            if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE)
            {
                $sql .= ' AUTO_INCREMENT';
            }
        }

        // don't add a comma on the end of the last field
        if (++$current_field_count < count($fields))
        {
            $sql .= ',';
        }
    }

    return $sql;
}

0
你可以在up方法的结束标签之前执行以下操作:
$this->db->query("ALTER TABLE `" . $this->db->dbprefix . $this->table_name . "` CHANGE `post_id` `post_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Unique post id'");

有没有其他方法在MySQL中不包括列定义的情况下完成它?没有

注意:

更改注释将导致表的完全重建。因此,您可能选择在非常大的表上不使用它。

最佳解决方案是创建、扩展或复制mysql或mysqli并重新实现_process_fields以处理字段的注释。这个链接可以帮助您入门,这个则是高级链接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接