I've got a task to create a migration for adding 2 instances of WorkRoom into database, I've already create a migration when created the Model and added to table 3 fields, I don't understand what migration should I do now.
class CreateWorkRoomsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('work_room', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->integer('building')->nullable();
$table->integer('floor')->nullable();
$table->integer('office_number')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('work_room');
Related
I'm having issues with the migration registering in the migrations table and I'm getting the following error when I run php artisan migrate:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key
constraint (SQL: alter table `surgeon_surgeon_specialty` add
constraint `surgeon_surgeon_specialty_surgeon_id_foreign` foreign key
(`surgeon_id`) references `surgeon` (`id`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Here's my files currently:
Surgeons Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeons', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('surgeon_name', 30)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('surgeons');
}
}
Surgeon Specialties Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSpecialtiesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeon_specialties', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('surgeon_specialty_name');
$table->timestamps();
$table->unique(['surgeon_specialty_name','user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('surgeon_specialties');
}
}
Then I used the Laravel-5-Generators-Extended package to generate the
Surgeon Surgeon Specialty Table Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSurgeonSpecialtyPivotTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeon_surgeon_specialty', function (Blueprint $table) {
$table->integer('surgeon_id')->unsigned()->index();
$table->foreign('surgeon_id')->references('id')->on('surgeon')->onDelete('cascade');
$table->integer('surgeon_specialty_id')->unsigned()->index();
$table->foreign('surgeon_specialty_id')->references('id')->on('surgeon_specialties')->onDelete('cascade');
$table->primary(['surgeon_id', 'surgeon_specialty_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Schema::drop('surgeon_specialty_surgeon');
Schema::drop('surgeon_surgeon_specialty');
}
}
But changed it to this per a friends suggestion:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSurgeonSpecialtiesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('surgeon_specialties', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('surgeon_specialty_name');
$table->timestamps();
$table->unique(['surgeon_specialty_name','user_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('surgeon_specialties');
}
}
The table does migrate...
...however, I am still getting the error. Any help would be much appreciated.Thanks!
I had this issue on Laravel 8. I'm not sure if it's relevant to this post or not. This happens when you create the migration for the many before the one part of one-to-many relationship. In your case I guess you created migration for items before users. Simply rename your migration files according to your relationships. Your surgeon migration date should be earlier to be created before surgeon_surgeon_specialty table.
Also I suggest to declare table name of Surgeon model:
class Surgeon extends Model {
public $table = 'surgeons';
}
This is because I see Laravel couldn't determine your table name based on your model class.
I have created a file Order.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
/**
* The table associated with the model.
* #var string
*/
protected $table = 'order';
}
Then I have created a migration, called 2016_01_01_111111_create_orders_table.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('order', function (Blueprint $table) {
$table->increments('id');
$table->string('orderIdent');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Then I did the migration with
php artisan migrate
Then, I wanted to add some fields, created a new migration file called 2016_01_02_111111_alter_orders.php:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AlterOrders extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::table('order', function(Blueprint $table)
{
$table->string('selecteddate');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Then again I did
php artisan migrate
But now I get this message in console:
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'order' already exists
What do I need to do, to add the new fields to my table?
Thanks in advance!
Adding this as an answer vs a comment to show what needs to be done. Since rollback was done, up needs to be commented out for now.
public function up()
{ /*
Schema::create('order', function (Blueprint $table) {
$table->increments('id');
$table->string('orderIdent');
$table->timestamps();
}); */
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('order');
}
Try this
public function up()
{
//
Schema::table('order', function($table)
{
$table->string('selecteddate');
});
}
remove Blueprint.
Try this :
php artisan migrate:refresh
I have found this question very similar to mine Make column not nullable in a Laravel migration though it is almost 3 years old and it surely does not belong to Laravel 5
My problem is that I have a migration which in the up function it modifies a column to make it nullable.
Now, in the down function I want to make it not nullable again.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* THIS IS WHAT I WOULD EXPECT TO DO
THOUGH OF COURSE THE FUNCTION notNullable DOES NOT WORK */
$table->string('mycolumn')->notNullable()->change();
});
}
I could achieve that using raw SQL, but I would like to do it using Laravel methods if it is possible... but I couldn't find it, probably it hasn't been implemented it in the version 5 either.
To reset it back to not nullable. You may try this.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* By default it's NOT NULL */
$table->string('mycolumn')->nullable(false)->change(); // <--- here
});
}
By default its NOT NULL, so you should try this.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('mytable', function(Blueprint $table) {
$table->string('mycolumn')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('mytable', function(Blueprint $table) {
/* By default it's NOT NULL */
$table->string('mycolumn')->change();
});
}
I created a users table using Laravel's schema builder. Here is the migration:
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::connection('mysql-internal')->create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('mysql-internal')->drop('users');
}
}
After running this migration, I then rolled it back using php artisan migrate:rollback. However, it looks like down method did not respect my connection. The users table was dropped from my default mysql database rather than the mysql-internal that I specified.
Did I do something wrong here? I am using Laravel 4.2.17.
I have two types of workbench migrations: creating regular tables and creating pivot tables for Many-to-Many relationships.
Sample of regular migration:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->increments('id')->unsigned();
$table->string('login')->unique();
$table->string('username')->unique();
$table->string('password');
$table->string('email');
$table->boolean('active')->default(true);
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Above migration can be rolled back
<?php
use Illuminate\Database\Migrations\Migration;
class CreatePivotRoleUser extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('role_user', function(\Illuminate\Database\Schema\Blueprint $table)
{
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->primary(['role_id', 'user_id']);
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
While this cannot, because it gives
"Class 'CreatePivotPermissionRole' not found"
error.
How to fix it?
Your code looks correct.
If CreatePivotPermissionRole is not found, it means it had been deleted before. Check the content of all your down() methods, there must be something wrong there.