yii2-gii icon indicating copy to clipboard operation
yii2-gii copied to clipboard

The generator makes all integer columns as […, 'default', 'value' => null] even column set as not null

Open Mirocow opened this issue 6 years ago • 1 comments

What steps will reproduce the problem?

We have some problem with pgsql generating model rules The generator makes all integer columns as […, 'default', 'value' => null] even column set as not null

What is the expected result?

What do you get instead?

Replace code from (https://github.com/yiisoft/yii2-gii/blob/master/src/generators/model/Generator.php#L358)

if ($driverName === 'pgsql' && $type === 'integer') {
    $rules[] = "[['" . implode("', '", $columns) . "'], 'default', 'value' => null]";
}

into

if ($driverName === 'pgsql' && $type === 'integer') {
    $defaultColumns = isset($types['required']) ? array_diff($columns, $types['required']) : $columns;
    if ($defaultColumns) {
        $rules[] = "[['" . implode("', '", $defaultColumns) . "'], 'default', 'value' => null]";
    }
}

Additional info

Q A
Yii version 2.0.16?
PHP version 7.2
Operating system Debian

Mirocow avatar Feb 26 '19 13:02 Mirocow

I've verified this and confirm that it generates the same default rule for null and not null columns. But, does anyone know what's the purpose of specifying a default value equals null for integers in postgresql? I mean why is this block of code needed?

if ($driverName === 'pgsql' && $type === 'integer') {
    $rules[] = "[['" . implode("', '", $columns) . "'], 'default', 'value' => null]";
}

alexkart avatar Apr 08 '19 11:04 alexkart