I’m developing a Laravel project and I created the models, when I run the migrations on my computer (where I’m developing) everything works fine, even the relations between tables. The problem comes when I upload the code to the production server and when I run the migrations where I create the relations, I got an error.
In the code below I leave some commented lines, those lines are things that I tried to do to solve the problem, but nothing worked.
I tried to set the table engine to innoDB on both tables, but it didn’t work.
I tried to set the id datatype as increments on both tables, and the column deviceTypeID that is going to serve as foreign key, I defined it as unsigned and integer and it didn’t work.
I even checked the PHP, Composer, Doctrine/dbal and mysql versions on both computers and are the same (or almosr the same).
Developing Computer:
PHP: 7.3
Composer: 1.8.6
MySQL Server: 5.7.23
Doctrine/dbal: 2.9
Production Server:
PHP: 7.3
Composer: 1.9
MySQL Server: 5.7.25
Doctrine/dbal: 2.9
(It runs Ubuntu Server 18.04)
I also tried to run the project on another computer an see if I got the same error, but it didn't come, everything worked fine.
I also ran this query "alter table devices add constraint devices_devicetypeid_foreign foreign key (deviceTypeID) references devicestype(id) on delete cascade" without all the quotes directly on mysql command line and it worked. So the problem is not related to the user privileges.
It always crash on production when it tries to run the code on AddRelationDevicesDeviceType
class CreateDevicesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devices', function (Blueprint $table) {
//$table->engine = 'innoDB'
$table->bigIncrements('id');
//$table->increments('id');
$table->unsignedBigInteger('deviceTypeID');
//$table->integer('deviceTypeID')->unsigned();
$table->timestamps();
});
}
}
class CreateDevicesTypeTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devicesType', function (Blueprint $table) {
//$table->engine = 'innoDB'
$table->bigIncrements('id');
//$table->increments('id');
$table->string("Device Type");
$table->text("Description");
$table->timestamps();
});
}
}
class AddRelationDevicesDeviceType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('devices', function($table) {
$table->foreign('deviceTypeID')
->references('id')->on('devicesType')
->onDelete('cascade');
});
}
}
This is the error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `devices` add constraint `devices_devicetypeid_foreign` foreign key (`deviceTypeID`) references `devicestype`(`id`) on delete cascade) at /var/www/html/MYPROJECT/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the databa se's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint") /var/www/html/MYPROJECT/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:119
2 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint") /var/www/html/MYPROJECT/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117
I I figured out how to solve my problem.
The problem was when I ran the first migration where I create the table “devicesType” MySQL changed the table name to lower case, so it ended up being “devicestype” and when I tried to refer that table in the last migration where I create the relation Laravel couldn’t find the table.
My development server wasn’t case sensitive while my production server was case sensituve.
I also change the "deviceTypeID" from "unsignedBigInteger" to "bigInteger" and I specify the the type unsigned using the method "unsigned"
This is my new code if is useful to someone:
class CreateDevicesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('deviceTypeID')->unsigned();
$table->timestamps();
});
}
}
class CreateDevicesTypeTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('devicestype', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("Device Type");
$table->text("Description");
$table->timestamps();
});
}
}
class AddRelationDevicesDeviceType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('devices', function($table) {
$table->foreign('deviceTypeID')
->references('id')->on('devicestype')
->onDelete('cascade');
});
}
}
Related
Good day. Please I ran into this error in my laravel app. I have a table of product. I want to add a 'url' column that will be unique. After running the migration, I get the error below. Please what is the way out of this? Thanks in anticipation.
Migrating: 2020_02_03_184424_add_url_to_products
Illuminate\Database\QueryException : SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'url' (SQL: alter table `products` add `url` varchar(255) not null)
at C:\xampp\htdocs\alvinsmakeup\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665
661| // If an exception occurs when attempting to run a query, we'll format the error
662| // message to include the bindings with SQL, which will make this exception a
663| // lot more helpful to the developer instead of just the database's errors.
664| catch (Exception $e) {
> 665| throw new QueryException(
666| $query, $this->prepareBindings($bindings), $e
667| );
668| }
669|
Exception trace:
1 PDOException::("SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'url'")
C:\xampp\htdocs\alvinsmakeup\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459
2 PDOStatement::execute()
C:\xampp\htdocs\alvinsmakeup\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459
Please use the argument -v to see more details.
Process finished with exit code 1 at 18:50:21.
Execution time: 1,796 ms.
This is my table schema
class AddUrlToProducts extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('products', function (Blueprint $table) {
//
$table->string('url')->unique();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('products', function (Blueprint $table) {
//
$table->dropColumn('url');
});
}
}
The column probably exists in your table. You can try to rollback the migration by running:
php artisan migrate:rollback
If the column does not disappear from your table. Delete it manually and rerun the migration by using:
php artisan migrate
Either you can check if the column is already exists and drop it before adding it or add it only when it is not available.
if (! Schema::hasColumn('products', 'url')) {
Schema::table('products', function (Blueprint $table) {
//
$table->string('url')->unique();
});
}
OR
if (Schema::hasColumn('products', 'url')) {
Schema::table('products', function (Blueprint $table) {
$table->drop('url');
});
}
Schema::table('products', function (Blueprint $table) {
//
$table->string('url')->unique();
});
Or if you have added this column in last migration, just rollbalck, make changes and run migration command again.
I have already checked the other topics but no solution worked with me. I tried the last time:
public function up()
{
if(Schema::hasColumn('orders_products_variations','id')) {
Schema::table('orders_products_variations',function (Blueprint $table) {
$table->unsignedInteger('id')->change();
$table->dropPrimary();
$table->dropColumn('id');
});
}
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('id');
});
}
And the error message is :
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: alter table orders_products_variations drop primary key)
Any idea?
A migration class contains two methods: up and down. The up method is
used to add new tables, columns, or indexes to your database, while
the down method should reverse the operations performed by the up
method.
Dropping Columns
To drop a column, use the dropColumn on the Schema builder inside down method
if (Schema::hasColumn('table_name', 'column_name')) {
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name');
});
}
Adding / Dropping Columns
PS When you have to add a new column to the existing table in Laravel, you must
create new migration .
php artisan make:migration add_column_to_table
I've got an issue where I'm running some tests in laravel 5.4 via phpunit
I'm using an in memory sqlite database for testing
I've got a test class which i've removed bunch of other stuff from so it effectively looks like
<?php
namespace Tests\Unit;
use App\User;
use App\Order;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class OrderTest extends TestCase
{
use DatabaseMigrations;
/** #test */
function can_update_status()
{
// This is empty, it fails on this test because its alphabetically the first test in the whole suite.
}
}
I've recently created a new migration which adds the "paid" column
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddStatusToOrders extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('completed');
$table->boolean('paid')->default(0);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->boolean('completed')->default(0);
$table->dropColumn('paid');
});
}
}
However whenever I run this test I get an error saying the paid column doesn't exist - even after a composer du
PHPUnit 6.0.7 by Sebastian Bergmann and contributors.
...................................E
Time: 10.69 seconds, Memory: 46.00MB
There was 1 error:
1) Tests\Unit\OrderTest::can_mark_as_paid
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such column: paid (SQL: update "orders" set "paid" = 1, "updated_at" = 2017-04-05 15:27:11 where "id" = 1)
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:607
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:477
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2145
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:768
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:581
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:501
/Users/owen/Sites/1st-choice-spares/app/Order.php:62
/Users/owen/Sites/1st-choice-spares/tests/Unit/OrderTest.php:95
Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such column: paid
/Users/owen/Sites/1st-choice-spares/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:79
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:470
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:640
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:607
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:477
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2145
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:768
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:581
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:501
/Users/owen/Sites/1st-choice-spares/app/Order.php:62
/Users/owen/Sites/1st-choice-spares/tests/Unit/OrderTest.php:95
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such column: paid
/Users/owen/Sites/1st-choice-spares/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:77
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:470
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:640
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:607
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:477
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2145
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:768
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:581
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:501
/Users/owen/Sites/1st-choice-spares/app/Order.php:62
/Users/owen/Sites/1st-choice-spares/tests/Unit/OrderTest.php:95
Does anybody have any ideas why this would be happening, and how I can resolve it? It might be worth adding I've tried changing the column names etc and the same issue is happening with that
Thanks
UPDATE
If I comment out the lines in the down migration e.g $table->dropColumn('paid');
Then it continues to run - however I'm struggling to understand why the down method would be running before the up has been run?
Update 2
It seems the above finding was due to the column not getting created in the first place, if i suppress that error, the original error appears that the column doesn't exist - which suggests the migration failed to create it.
According to laravel documentation
Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported.
And although you not trying to modify or drop multiple columns ,you are trying to drop and create in one single migration and in both cases ALTER TABLE query is executed ,and the problem here is the limitations of ALTER TABLE query of sqlite .
You can separate each statement like this:
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('completed');
});
Schema::table('orders', function (Blueprint $table) {
$table->boolean('paid')->default(0);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->boolean('completed')->default(0);
});
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('paid');
});
}
here is the issue. I am banging my head for hours now on this. Laravel is not allowing me to have a primary key (it says multiple but you can see it has only one). And further more as there is issue causing in primary key in teachers table. I am not able to use this as a foreign key in courses table.
Migration file for teacher table
class CreateTeachersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('teachers', function (Blueprint $table) {
$table->increments('teacher_id')->primary();
$table->string('email', 200);
$table->string('first_name',300)->nullable();
$table->string('last_name',300)->nullable();
$table->string('account_status',50)->default('inactive');
$table->string('subscription_status',50);
$table->string('account_reset_code',400)->nullable();
$table->string('passkey',500);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('teachers');
}
}
Migration File for courses table
class CreateCoursesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('course_id')->primary();
$table->integer('teacher_id_fr');
$table->string('course_name', 500);
$table->string('course_code',300);
$table->string('subject_area',300);
$table->string('course_level',100);;
$table->string('grade_periods',100);
$table->timestamps();
});
Schema::table('courses',function(Blueprint $table){
$table->foreign('teacher_id_fr')->references('teacher_id')->on('teachers')->onDelete('cascade')->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('courses');
}
}
Issue raised on migrating
Syntax Error or access violation. 1068 Multiple primary keys found
.
What I've tried by now
1) Making the primary column unsigned using unsigned method.
2) using InnnoDB engine type for table.
3) And much other things as suggested for similiar posts on the forums but nothing seems to work for me.
EDIT
I've removed the primary() method from both files at $table->increments('teacher_id'). As this is said Laravel marks auto increments columns as primary keys.
But now the error is on courses table as it says.
General error : 1215 cannot add foreign key constraint
You can define Multiple primanry key in migration file as below:
$table->primary(array('field_name1', 'field_name2'));
Try to replace this, remove primary(), increments automatically creates it primary
$table->increments('teacher_id')->primary();
to
$table->increments('teacher_id');
For new error try this General error : 1215 cannot add foreign key constraint
public function up()
{
Schema::create('courses', function (Blueprint $table) {
$table->increments('course_id');
$table->integer('teacher_id_fr');
$table->string('course_name', 500);
$table->string('course_code',300);
$table->string('subject_area',300);
$table->string('course_level',100);;
$table->string('grade_periods',100);
$table->timestamps();
$table->foreign('teacher_id_fr')
->references('teacher_id')->on('teachers')
->onDelete('cascade')
->onUpdate('cascade');
});
}
I was able to fix the issue with a minute change. I modified courses migration file like below and it worked like a charm
$table -> integer('teacher_id_fr') -> unsigned();
Reason
Actually both the referenced and referencing columns (teacher.teacher_id and course.teacher_id_fr in this case) must be of same type.
Having said that, Laravel marks autoincrement columns as "Primary" and of type unsigned. So make sure to change foregin key column to match the type of primary key i.e unsigned. which the above modification does.
Hope it clarifies the issue and solution. If you have anything else on your mind. Please give it a shot here.
Happy Learning!
I've created a table using migration like this:
public function up()
{
Schema::create('despatch_discrepancies', function($table) {
$table->increments('id')->unsigned();
$table->integer('pick_id')->unsigned();
$table->foreign('pick_id')->references('id')->on('picks');
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
$table->integer('original_qty')->unsigned();
$table->integer('shipped_qty')->unsigned();
});
}
public function down()
{
Schema::drop('despatch_discrepancies');
}
I need to change this table and drop the foreign key reference & column pick_detail_id and add a new varchar column called sku after pick_id column.
So, I've created another migration, which looks like this:
public function up()
{
Schema::table('despatch_discrepancies', function($table)
{
$table->dropForeign('pick_detail_id');
$table->dropColumn('pick_detail_id');
$table->string('sku', 20)->after('pick_id');
});
}
public function down()
{
Schema::table('despatch_discrepancies', function($table)
{
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
$table->dropColumn('sku');
});
}
When I run this migration, I get the following error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of
'./dev_iwms_reboot/despatch_discrepancies' to
'./dev_iwms_reboot/#sql2-67c-17c464' (errno: 152) (SQL: alter table
despatch_discrepancies drop foreign key pick_detail_id)
[PDOException]
SQLSTATE[HY000]: General error: 1025 Error on rename of
'./dev_iwms_reboot/despatch_discrepancies' to
'./dev_iwms_reboot/#sql2-67c-17c464' (errno: 152)
When I try to reverse this migration by running php artisan migrate:rollback command, I get a Rolled back message, but it's not actually doing anything in the database.
Any idea what might be wrong? How do you drop a column that has a foreign key reference?
You can use this:
Schema::table('despatch_discrepancies', function (Blueprint $table) {
$table->dropForeign(['pick_detail_id']);
$table->dropColumn('pick_detail_id');
});
If you take a peak at dropForeign source, it will build the foreign key index name for you if you pass the column name as an array.
It turns out; when you create a foreign key like this:
$table->integer('pick_detail_id')->unsigned();
$table->foreign('pick_detail_id')->references('id')->on('pick_details');
Laravel uniquely names the foreign key reference like this:
<table_name>_<foreign_table_name>_<column_name>_foreign
despatch_discrepancies_pick_detail_id_foreign (in my case)
Therefore, when you want to drop a column with foreign key reference, you have to do it like this:
$table->dropForeign('despatch_discrepancies_pick_detail_id_foreign');
$table->dropColumn('pick_detail_id');
Update:
Laravel 4.2+ introduces a new naming convention:
<table_name>_<column_name>_foreign
Update:
Larave > 8.x introduces a new function
dropConstrainedForeignId('pick_detail_id');
This will delete the column as well as the foreign key of the column
I had multiple foreign keys in my table and then I had to remove foreign key constraints one by one by passing column name as index of the array in down method:
public function up()
{
Schema::table('offices', function (Blueprint $table) {
$table->unsignedInteger('country_id')->nullable();
$table->foreign('country_id')
->references('id')
->on('countries')
->onDelete('cascade');
$table->unsignedInteger('stateprovince_id')->nullable();
$table->foreign('stateprovince_id')
->references('id')
->on('stateprovince')
->onDelete('cascade');
$table->unsignedInteger('city_id')->nullable();
$table->foreign('city_id')
->references('id')
->on('cities')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('offices', function (Blueprint $table) {
$table->dropForeign(['country_id']);
$table->dropForeign(['stateprovince_id']);
$table->dropForeign(['city_id']);
$table->dropColumn(['country_id','stateprovince_id','city_id']);
});
}
Using below statement does not work
$table->dropForeign(['country_id','stateprovince_id','city_id']);
Because dropForeign does not consider them seperate columns that we want to remove. So we have to drop them one by one.
The key (for me) to solving this was to make sure that the $table->dropForeign() command was being passed the right relationship name, not necessarily the column name. You do not want to pass the column name, as would be much more intuitive IMHO.
What worked for me was:
$table->dropForeign('local_table_foreign_id_foreign');
$table->column('foreign_id');
So the string I passed to dropForeign() that worked for me was in the format of:
[local table]_[foreign key field]_foreign
If you have access to a tool like Sequel Pro or Navicat, being able to visualize those will be very helpful.
Something that occurred to me was that I didn't know where to put the Schema::table block.
Later I discovered that the key is on the SQL error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists `lu_benefits_categories`)
So the Schema::table block needs to go in the down() function of the lu_benefits_categories migration and before the Schema::dropIfExists line:
public function down()
{
Schema::table('table', function (Blueprint $table) {
$table->dropForeign('table_category_id_foreign');
$table->dropColumn('category_id');
});
Schema::dropIfExists('lu_benefits_categories');
}
After that, the php artisan migrate:refresh or php artisan migrate:reset will do the trick.
on laravel 8 use dropConstrainedForeignId
(https://github.com/laravel/framework/pull/34806)
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddAddressFieldsInEventTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('events', function (Blueprint $table) {
$table->bigInteger('address_id')->nullable();
$table->foreign('address_id')
->references('id')
->on('addresses')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('events', function (Blueprint $table) {
$table->dropConstrainedForeignId('address_id');
$table->dropColumn('address_id');
});
}
}
You can disable relation id first
Schema::disableForeignKeyConstraints();
In Laravel > 8.x you can use the following code to add/remove foreign keys. You will need to drop the foreign key before dropping the column, as otherwise, the foreign key could cause the error your encountering.
Add foreign key
$table->foreignId('pick_id')->constrained('picks')->cascadeOnUpdate()
->cascadeOnDelete();
Drop foreign key
$table->dropConstrainedForeignId('pick_id');