yii2 migration, use sql check constraint when creating table

I want to use migration to create table in yii2.0.6. But i’m facing a difficulty the tables are created in my database but the check constraint doesn’t work. That’s when i enter a value that should be.rejected, the DBMS accepts it without any error.

I read the comment of the check(string$check),method in yii/db/ColumnSchemaBuilder class. It is say that the $check attribute is the SQL of the check contraint but i don’t have example of using it. Could someone help me?regards!

This is what i write in my up() function:

$this->createTable('{{℅user}}'), [

'id'=>$this->primaryKey (),


'role'=>$this->string(10)->check("role in ('admin', 'member'))"

Please i’m waiting for an idea guys.

Does your DBMS support CHECK constraints (hint: MySQL doesn’t)?

I totally missed it! Thanks, i’m usibg MySQL.

I’ll use rules to check that in my activeReccord. It’s a good way?


I have the same issue. What’s the best way to implement this?