keep getting error when trying to add foreign key constraints - laravel

I have the book migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('title');
$table->string('Author');
$table->text('Book_description');
$table->integer('user_id');
$table->foreign('user_id')->refrences('id')->on('users');
$table->boolean('donated')->default(false);
$table->string('country',150);
$table->string('city',30);
$table->string('path');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
but when I run the following command:
php artisan migrate:refresh
I get the following error:
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 (SQL: alter table books add constraint books_user_id_foreign foreign key (user_id) references users ())
I have searched Google but could not find what is wrong and also I have read Laravel 5.6 docs but I could not figure out what is wrong.

There's a typo: It has to be ->references('id') instead of ->refrences('id').

Try with this
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
});
Schema::table('books', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}

Related

Laravel Migration: Can't add FK to both string types

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan I am thrown the following error:
Copy\vendor\laravel\framework\src\Illuminate\Database\Connection.php:465
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'unsigned not null, `updated_by` varchar(255) unsigned not null, `enabled` var...' at line 1")
Lessons migration table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLessonTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('lesson', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->bigInteger('module_id')->unsigned();
$table->text('content');
$table->string('created_by')->unsigned();
$table->string('updated_by')->unsigned();
$table->string('enabled');
$table->string('position');
$table->timestamps();
});
Schema::table('lesson', function(Blueprint $table) {
$table->foreign('module_id')->references('id')->on('modules');
$table->foreign('created_by')->references('username')->on('admins');
$table->foreign('updated_by')->references('username')->on('admins');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('lesson');
}
}
Admins Migration Table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAdminsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('username');
$table->string('password');
$table->string('fname');
$table->string('mname');
$table->string('lname');
$table->string('email');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('admins');
}
}
I was trying to connect the created_by and updated_by columns of the Lessons table to the Admins table column 'username'. Both are in string already.
The problem is you're using unsigned on a column you're defining as a string, you can't have an unsigned string. As you're attempting to create a FK relationship between lessons and users, you likely want to use an integer column of some sort.
You could write your migration as follows:
public function up()
{
Schema::create('lesson', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->string('enabled');
$table->string('position');
$table->unsignedBigInteger('module_id');
$table->unsignedBigInteger('created_by');
$table->unsignedBigInteger('updated_by')->nullable();
$table->foreign('module_id')->references('id')->on('modules');
$table->foreign('created_by')->references('id')->on('admins');
$table->foreign('updated_by')->references('id')->on('admins');
$table->timestamps();
});
}
$table->string('created_by')->unsigned();
I think the problem is that unsigned is for numbers only not string
remove unsigned and try again

I cant migrate, what shoud I do( laravel-5.8 ) ? SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

1.why cant I migrate ?
I wanted to crate role permission table, but i cant add foreign key, i got this error : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
i tried to migrate, but got some errors
i dont know what should i do in laravel 5.8
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRoleAndPermissionTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title')->nullable();
$table->timestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('title')->nullable();
$table->timestamps();
});
**Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->primary(['role_id','user_id']);
});
2.it just migrate upper tables,It wont migrate this table ?
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
$table->primary(['permission_id','role_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_and_permission');
}
}
Comment or remove all $table->foreign('') from all migration. It will work.
The best solution is to use $table->bigIncrements('') on primary key rather than $table->increments('') and $table->unsignedBigInteger(''); for foreign keys rather than $table->integer('');.
Because Laravel 5.8 uses $table->bigIncrements('') on primary key and $table->unsignedBigInteger(''); for foreign keys.

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in laravel

can someone please help me with this one? I'm new to Laravel and when I try to do:
php artisan migrate
I get the error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table posts add constraint posts_author_id_foreign foreign key (author_id) references users (id) on delete restrict)
and this is the post table content:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
I have noticed that when I comment this line:
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
the error is gone, so I guess the problem is in that line. So, can someone please tell me what will be the correct syntax or how to fix it?
The column data type must be the same on both tables for the foreign key to work. By default, the users id column is:
$table->bigIncrements('id');
Therefore, you need to use bigInteger for your posts migration:
$table->bigInteger('author_id')->unsigned();
Try to change the foreign ko to unsignedInteger
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('author_id');
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('title');
$table->string('slug')->unique();
$table->text('excerpt');
$table->text('body');
$table->string('image')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}

Laravel Cannot update or delete a parent row a foreign key constraint fails

i am trying to run command php artisan migrate:rollback and it throw me the error cannot update or delete a parent row foreign key constraint fails
there is now issue when i run command php artisan migrate it successfully migrate my all tables but when i run rollback command it throw me the error the error is on my purpose_of_visits migration
public function up()
{
Schema::create('purpose_of_visits', function (Blueprint $table) {
$table->increments('id');
$table->string('purpose', 100);
$table->string('description', 197);
$table->integer('speciality_id')->unsigned()->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
$table->softDeletes();
$table->integer('created_by')->unsigned()->nullable();
$table->integer('updated_by')->unsigned()->nullable();
$table->foreign('speciality_id')->references('id')->on('specialities')->onDelete('cascade');
$table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
$table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('purpose_of_visits');
}
and my specialities migration:
public function up()
{
Schema::create('specialities', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
$table->string('description',250)->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
$table->softDeletes();
$table->integer('created_by')->unsigned()->nullable();
$table->integer('updated_by')->unsigned()->nullable();
$table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
$table->foreign('updated_by')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('specialities');
}
i cant figure out where is the issue even i am using onDelete('cascade')
your help will be highly appreciated!
Make sure you have speciality_id, created_by and updated_by in the fillable property of your purpose_of_visits model. See docs here.
For example on your model.
protected $fillable = ['speciality_id','created_by','updated_by'];
Remove the foreign key constraints of the table before dropping it.
public function down()
{
Schema::table('purpose_of_visits', function (Blueprint $table) {
$table->dropForeign(['speciality_id']);
$table->dropForeign(['created_by']);
$table->dropForeign(['updated_by']);
});
Schema::dropIfExists('purpose_of_visits');
}
Sorry For the Late reply There are Two Situation Where this error can be thrown
For Eg:
I have tables such as posts, authors
And here is my post table Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->unsignedInteger('author_id');
$table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
and here is my authors table migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAuthorsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('authors');
}
}
Situation 1:
now if the posts table migration runs before the authors table migration it my throw the error
Situation 2:
in some cases if you miss unsigned it may throw error
Solution1:
use
$table->unsignedInteger('speciality_id');
$table->unsignedInteger('speciality_id');
$table->foreign('author_id')->references('id')->on('specialities')->onDelete('cascade');
instead of this
$table->integer('speciality_id')->unsigned()->nullable();
$table->foreign('speciality_id')->references('id')->on('specialities')->onDelete('cascade');
if it again fails use this
try composer dumpautoload
adn then
Schema::disableForeignKeyConstraints();
At the beggining of the migration
and at the end
Schema::enableForeignKeyConstraints();
eg: you migration may look like
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
Schema::enableForeignKeyConstraints();
}
and if the same error throws please attach Error Screenshot and commet below
Hope it helps

Dropping foreign keys in Laravel migration

I have a problem with dropping some foreign keys from my Laravel application. The problem is when I am trying to rollback the migration:
php artisan migrate:rollback
I don't know why I have errors in the console:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists (SQL: alter table role_user drop foreign key role_user_user_id_foreign)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'role_user_user_id_foreign'; check that column/key exists
Below I'm showing my migration class:
class UpdateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
schema::table('role_user',function(Blueprint $table){
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('role_id')->references('id')->on('roles');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign('role_user_user_id_foreign');
$table->dropForeign('role_user_role_id_foreign');
});
}
}
My table in the database has been created by migration class:
class CreateRoleUserTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('role_user');
}
}
In all of the >4.0 versions of Laravel, it allows placing column names into an array, which it will then resolve on its own. I tried to find accompanying docs, but they seem to have left it out.
In your update migration, try this:
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['role_id']);
});
I just had this issue and the problem was due to the fact that $table->dropForeign([column_name]); drops the index and not the column itself. The solution is in the down function drop the index in one block and then drop the actual column in a separate block. They have to be dropped in separate blocks because of something to do with not being able to drop the key in the same connection as where the column is being dropped.
So the down function should look like this:
public function down() {
// drop the keys
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['role_id']);
});
// drop the actual columns
Schema::table('role_user', function (Blueprint $table) {
$table->dropColumn('user_id');
$table->dropColumn('role_id');
});
}
now you can run php artisan migrate to run the up function and php artisan migrate:rollback to run the down command and the error no longer shows.
I have modified your code below.
Add the onDelete() and onUpdate() to your code.
public function up()
{
Schema::table('role_user',function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE')->onUpdate('CASCADE');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('CASCADE')->onUpdate('CASCADE');
});
}
public function down() {
Schema::table('role_user', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['role_id']);
});
}
In Laravel 9.x, you do not need to specify a separate deletion of foreign keys.
<?php
use App\Models\Question;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up(): void
{
Schema::create('answers', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Question::class, 'question_id');
$table->text('correctly');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down(): void
{
Schema::dropIfExists('answers');
}
};

Resources