Adding comments to columns in codeigniter migrations

I am writing a lot of migration scripts for the database for my application. I would like to add comments for the columns so that others can easily recognize the column's content. One option is to write normal SQL query and add the comment. But is there a way I can add these comments inside the Migration scipt?

$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,
  )
));

This is the basic code that I have written. May have some syntax error. But I just copy pasted it.

Can anyone please help.

CodeIgniter added this ability with version 3.0. You can add comments using the 'comment' key:

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

Adding comments to fields in migrations, protected $_comment = ' COMMENT '; . . . /** * Process column * * @param array $field * @return string */ Use add_column rather than add_field. add_field is used to define fields for table creation, add_column modifies an existing table. The docs are pretty confusing on that point.

Looking at the CodeIgniter core, specifically system/database/drivers/mysql/mysql_forge.php, it looks like the COMMENT field isn't supported.

For reference, here is the function that parses the fields array out:

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;
}

Option to add column comment � Issue #5 � rowend/codeigniter , Is there any option to add comment? rowend / codeigniter-migrations-mysql Now there is no option to add a comment to column. But in the� Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You would also have to keep track of which changes need to be

What you can do is the following in the up method just before the closing tag of it:

$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'");

Is there any other way to do it without including the column definition in MySQL? No

Note:

Altering a comment will cause a full resconstruction of the table. So you may choose to live without it on very big table.

The Best solution would be to create, extend or copy the mysql or mysqli and reimplementing the _process_fields to handle the comment of the fields. This link can help get you started and this is the advanced.

How to set a comment on table column using Schema Builder, I'd like to add comments on some table columns so I can re-think or communicate to other Just put that info in the migration? <?php namespace Illuminate\ Database\Schema\Grammars; use Illuminate\Support\Fluent; use� Add a single Column an existing table; Add Multiple Columns in Existing Table; Available Column Types; Let’s have a table called notes where the table you want to add notes type. Add a single Column an existing table. To add a new column to the existing table using the laravel migration. Let’s open your terminal and create a migration file using the below command: php artisan make:migration add_type_to_notes. It will create an add_type_to_notes file in the migration folder.

Database migrations with CodeIgniter, CI needs you to edit application/config/migrations.php to change $config[' migration_enabled'] = FALSE; to $config['migration_enabled'] It will have an 'id' column. We're also adding a date field, a location field, and a text description. Please enable JavaScript to view the comments powered by Disqus. Codeigniter also comes with builtin migration feature and we are going to learn how to steup and run database migration. By default, migration is disabled in codeigniter.

Database: Migrations - Laravel, For example, to make the column "nullable", you may use the ->comment('my comment'), Add a comment to a column� Using CodeIgniter; General Help; can't add columns to existing table with migration; Thread Modes. can't add columns to existing table with migration. codemicky

Database Forge Class — ExpressionEngine 5.3.2 Documentation, Once initialized you will access the methods using the ee()->dbforge object: Add fields, add keys to the table, alter columns. CodeIgniter provides a� We create a file named: 20150724122000_rename_name_field_add_last_name_in_users_table.php (this is like the contest for the longest file names…) To change the definition of the columns we simply call the modify_column() method of the dbforge class, and pass it the table name and the columns we want to change. To create the new definitions we

Comments
  • what happened when you tried it?
  • the code works fine. But I am not able to add comments to the columns which I can always do through the phpmyadmin interface.
  • Guys please help. Although this is not stopping my development, but I would like to have this functionality in place.
  • I have tried "comment"/"comments" and while it doesn't cause an error it does nothing. Has nobody else ran into this issue?