Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying unsigned attribute when using the Schema Builder Trait in MIgrations?

Tags:

yii2

I have to specify a column as unsigned in yii2 migrations .
Example migration code from manual

public function up()
{
    $this->createTable('news', [
        'id' => $this->primaryKey(),
        'title' => $this->string()->notNull()
    ]);
}

From the research I have done there doesn't seem to be a method to add the unsigned capability in schema builder trait.

But is there some other way I can add unsigned attribute to the column while still making use of the schemaBuilderTrait style methods ?

For instance the $this->string() above returns an instance of yii\db\ColumnSchemaBuilder, but that doesn't even have a property to set unsigned/signed..

like image 657
gyaani_guy Avatar asked Sep 01 '15 10:09

gyaani_guy


2 Answers

Unfortunately, some things are impossible to write with new migrations syntax.

In this case you can use string concatenation like that:

'title' => $this->string()->notNull() . ' UNSIGNED',

Alternatively you can use old syntax (backwards compatibility is observed):

use yii\db\Schema;

...

'title' => Schema::TYPE_STRING . ' NOT NULL UNSIGNED',

P.S. You can post issue on official framework repo for this problem.

Update: It's already implemented, use ->unsigned() method. Note that you need to update framework. Thanks leitasat for information.

like image 81
arogachev Avatar answered Nov 07 '22 05:11

arogachev


Just in case: they did it.

Now you can add ->unsigned() to your definition.

like image 32
leitasat Avatar answered Nov 07 '22 07:11

leitasat