I need to change two fields from integer to foreign key. How do I build my migration to do so?
Schema::create('messages', function($table)
{
$table->increments('id');
$table->integer('sender');
$table->integer('recipient');
$table->string('title');
$table->longtext('body');
$table->timestamps();
$table->softDeletes();
$table->integer('regarding');
});
I'll change sender
to sender_id
, recipient
to recipient_id
and regarding
to regarding_id
.
$table->unique('slug'); So you add unique index to existing 'slug'.
The code to change the column to nullable is as "nullable()->change()". ->nullable()->change(); For this example, we'll be using the route body and we will make the "posts" table "user_id" to be nullable.
Don't think too much about it, I'm using following code. Here I change the data type from varchar to text.
public function up(){
DB::statement('ALTER TABLE items MODIFY COLUMN item_description TEXT');
DB::statement('ALTER TABLE items MODIFY COLUMN delivery_description TEXT');
}
public function down(){
DB::statement('ALTER TABLE items MODIFY COLUMN item_description VARCHAR(255)');
DB::statement('ALTER TABLE items MODIFY COLUMN delivery_description VARCHAR(255)');
}
I came across a similar problem needing to change a column type from string to integer. I managed this using two separate migrations (with a RAW sql statement in each) to get the solution.
In addition this works with Postgres and MySQL because we were in the middle of a transfer.
The first migration:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('plans', function(Blueprint $table)
{
//
$table->mediumInteger('duration_change_type')->default(0)->after('duration');
});
if (Config::get('database')['default'] === 'mysql'){
// Mysql
DB::statement('update plans set duration_change_type=duration');
} else if (Config::get('database')['default'] === 'pgsql'){
// PostgreSQL
DB::statement('update plans set duration_change_type=duration::integer');
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('plans', function(Blueprint $table)
{
//
$table->dropColumn('duration_change_type');
});
}
The second migration:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('plans', function(Blueprint $table)
{
//
$table->dropColumn('duration');
$table->renameColumn('duration_change_type', 'duration');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('plans', function(Blueprint $table)
{
// Rollback to string
$table->string('duration_change_type')->default(0)->after('duration');
});
if (Config::get('database')['default'] === 'mysql'){
// Mysql
DB::statement('update plans set duration_change_type=duration');
} else if (Config::get('database')['default'] === 'pgsql'){
// PostgreSQL
DB::statement('update plans set duration_change_type=duration::text');
}
}
Thinking on this now it could be simplified to a single migration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With