Yii2: yii\db\ColumnSchema::typecast() converts empty string to null even if column doesn't allow null

Created on 5 Jun 2018  路  4Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

  1. Create a MySQL database table with a text column.
  2. Run this:

    Yii::$app->db->createCommand()->insert('table_name', [
       'column_name' => '' // empty string
    ])->execute();
    

What is the expected result?

The row should be inserted.

What do you get instead?

A yii\db\IntegrityException exception is thrown because the empty string gets converted to null by yii\db\ColumnSchema::typecast(), despite the ColumnSchema鈥檚 $allowNull property being set to false.

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.15.1

MySQL ready for adoption bug

Most helpful comment

Nevermind, I misunderstood what the code was doing (missed the ! before in_array(). Bug ended up being due to Craft adding support for additional text column types (mediumtext, etc.). So it makes sense that the fix should only be implemented in Craft: craftcms/cms@c3568e4e444cfcb0d73a0ea553cd0bae183f2d07

All 4 comments

I fixed this in Craft by extending yii\db\ColumnSchema and overriding the typecast() method: craftcms/cms@6140530a69692a90d08e5eacb24cdfe0b984edb9

Would you like to make a pull request to Yii?

done :)

Nevermind, I misunderstood what the code was doing (missed the ! before in_array(). Bug ended up being due to Craft adding support for additional text column types (mediumtext, etc.). So it makes sense that the fix should only be implemented in Craft: craftcms/cms@c3568e4e444cfcb0d73a0ea553cd0bae183f2d07

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kminooie picture kminooie  路  3Comments

sobit picture sobit  路  3Comments

MUTOgen picture MUTOgen  路  3Comments

SamMousa picture SamMousa  路  3Comments

Locustv2 picture Locustv2  路  3Comments