Laravel migration change and make column nullable -
i created migration user_id unsigned. how can edit user_id in new migation make nullable()
?
schema::create('throttle', function(blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); // needs nullable, how should next migration be? }
i assume you're trying edit column have added data on, dropping column , adding again nullable column not possible without losing data. we'll alter
existing column.
however, laravel's schema builder not support modifying columns other renaming column. need run raw queries them, this:
function up() { db::statement('alter table `throttle` modify `user_id` integer unsigned null;'); }
and make sure can still rollback migration, we'll down()
well.
function down() { db::statement('alter table `throttle` modify `user_id` integer unsigned not null;'); }
one note since converting between nullable , not nullable, you'll need make sure clean data before/after migration. in migration script both ways:
function up() { db::statement('alter table `throttle` modify `user_id` integer unsigned null;'); db::statement('update `throttle` set `user_id` = null `user_id` = 0;'); } function down() { db::statement('update `throttle` set `user_id` = 0 `user_id` null;'); db::statement('alter table `throttle` modify `user_id` integer unsigned not null;'); }
Comments
Post a Comment