Skip to content

DDL create/alter does not provide DEFAULT NULL for nullable columns #140

@simon-mundy

Description

@simon-mundy

Package Version

0.6.0

Php Version

8.3

Database Engine

MySQL

Basic Information

When using the DDL scripts, it would be the most sane default to allow nullable columns to have a NULL default value. However if nullable is true and no default is given, no default value is assigned when creating the SQL.

Steps to Reproduce

use PhpDb\Sql\Ddl\Column\Varchar;
use PhpDb\Sql\Ddl\CreateTable;

$table = new CreateTable('users');
$table->addColumn(
    (new Varchar('mycol', 100))
        ->setNullable(true)
);

echo $table->getSqlString();

Expected Behavior

CREATE TABLE `users` (
    `mycol` VARCHAR(100) NULL DEFAULT NULL
)

Actual behavior?

CREATE TABLE `users` (
    `nickname` VARCHAR(100)
)

Additional Info

It seems this behaviour has been present since Laminas Db days

Also note that if setDefault() is present it will use that value instead, so adding setDefault('myval') will override this.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingconfirmedHas been confirmed by maintainers

Type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions