Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addColumn yii migration position

I want to add a column at the seventh place in the table, I am using

$this->addColumn('table_name','column_name','type'); 

adds the column at the end. Is there any way where I can mention the place to add column? Or any after column keyword to add my new column after, for exapmle, password column. I have learnt aboout migration from Yii Doc

like image 291
dibs_ab Avatar asked Sep 20 '12 04:09

dibs_ab


People also ask

What is migration in yii?

Yii provides the database migration feature that allows you to keep track of database changes. Yii provides the following migration command line tools − Create new migrations. Revert migrations. Apply migrations.

How do I run a specific migration in yii2?

To run specific migration, you can mark(skip) migrations upto just before one you want run. You can mark migration by using one of following command: Using timestamp to specify the migration yii migrate/mark 150101_185401. Using a string that can be parsed by strtotime() yii migrate/mark "2015-01-01 18:54:01"


2 Answers

This should work!

$this->addColumn('table_name', 'column_name', 'type AFTER column6'); 

examples:

$this->addColumn('tbl_posts', 'email', 'VARCHAR(150) AFTER `name` ');
$this->addColumn('tbl_posts', 'phone', 'string AFTER `email` ');
like image 129
SuVeRa Avatar answered Oct 12 '22 00:10

SuVeRa


$this->addColumn('{{%user}}', 'username', 
            $this->string()->notNull()->unique()->after('id')
            );
like image 43
gvanto Avatar answered Oct 11 '22 22:10

gvanto