Remove UNIQUE from column using yii2 migrations

I have a unique column, how to remove the UNIQUE key from this column using hyphenation.

I am using the latest version of yii 2

public function up()
{
    $this->alterColumn('user', 'email', $this->string(255)->notNull());

}

public function down()
{
    $this->alterColumn('user', 'email', $this->string(255)->notNull()->unique());
}

Changing a column as such does not work

+4
source share
1 answer

How sql to create a unique index is something like this

//sql to add a unqique index
ALTER TABLE `user` ADD UNIQUE (
`email`
);

//sql to remove a unqique index
ALTER TABLE 'user' DROP INDEX email;

just use dropIndex () and remove the unique index.

username user, email, , . , / .

public function up()
{
    // remove the unique index
    $this->dropIndex('username', 'user');
}

public function down()
{
    // add the unique index again
    $this->createIndex('username', 'user', 'username', $unique = true );
}
+3

Source: https://habr.com/ru/post/1627315/


All Articles