Skip to content

Bug: [Forge] addColumn() does not set NOT NULL by default #7235

@kenjis

Description

@kenjis

PHP Version

8.1

CodeIgniter4 Version

develop (5ffe732) after 4.3.1

CodeIgniter4 Installation Method

Git

Which operating systems have you tested for this bug?

macOS

Which server did you use?

cli-server (PHP built-in webserver)

Database

MySQL 5.7

What happened?

addColumn() does not set NOT NULL by default.

Steps to Reproduce

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateTableTestTable extends Migration
{
    public function up()
    {
        $this->forge->addField(
            [
                'col1' => ['type' => 'VARCHAR', 'constraint' => 255],
                'col2' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
                'col3' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
            ]
        );
        $this->forge->createTable('test_table');
    }

    public function down()
    {
    }
}
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddColumnToTestTable extends Migration
{
    public function up()
    {
        $this->forge->addColumn('test_table', [
            'col4' => ['type' => 'VARCHAR', 'constraint' => 255],
            'col5' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'col6' => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => false],
        ]);
    }

    public function down()
    {
    }
}
$ php spark migrate
create table test_table
(
    col1 varchar(255) not null, -- NOT NULL by default
    col2 varchar(255) null,
    col3 varchar(255) not null,
    col4 varchar(255) null, -- NULL by default
    col5 varchar(255) null,
    col6 varchar(255) not null
)
    charset = utf8;

Expected Output

    col4 varchar(255) not null,

Anything else?

The addColumn() method is used to modify an existing table. It accepts the same field array as above, and can be used for an unlimited number of additional fields.
https://codeigniter4.github.io/userguide/dbmgmt/forge.html#forge-addcolumn

And in the above:

null/true : to generate “null” in the field definition. Without this, the field will default to “NOT null”.
https://codeigniter4.github.io/userguide/dbmgmt/forge.html#adding-fields

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseIssues or pull requests that affect the database layer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions