Not making a table in Laravel 7 - laravel

I have tried different ways to solve this problem, but none of them have solved the problem.This part of the code is for building a relationship in Laravel, but this code works in Laravel 5 but is error-prone in Laravel 7.
Error:
SQLSTATE[HY000]: General error: 1005 Can't create table shoplaravel.role_user (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table role_user add constraint role_user_role_id_foreign foreign key (role_id) references roles (id) on delete cascade on update cascade)
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->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$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']);
});
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('permission_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade')
->onUpdate('cascade');
$table->primary(['permission_id', 'role_id']);
});

Foreign keys need to be of the exact same type as the column they're referencing.
Laravel's default user table migration uses $table->increments('id'); - an alias of $table->bigIncrements('id').
Change $table->increments('id'); to $table->bigIncrements('id'); in your roles and permissions table migrations.
More on migration columns: https://laravel.com/docs/7.x/migrations#creating-columns

Remove BigInteger and use only unsignedInteger
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->unsignedInteger('role_id');
$table->unsignedInteger('user_id');
$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']);
});
Schema::create('permission_role', function (Blueprint $table) {
$table->unsignedInteger('role_id');
$table->unsignedInteger('permission_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('permission_id')
->references('id')
->on('permissions')
->onDelete('cascade')
->onUpdate('cascade');
$table->primary(['permission_id', 'role_id']);
});

Related

Laravel Foreign key constraint is incorrectly formed after adding $table->engine = 'MyISAM';

After adding $table->engine = 'MyISAM'; seem foreign key is not working anymore. This my main table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'MyISAM';
$table->integer('id');
$table->string('user_id',20);
$table->string('name');
$table->primary(['user_id','id']);
$table->rememberToken();
$table->timestamps();
});
DB::statement('ALTER TABLE users CHANGE id id INT(11) NOT NULL
AUTO_INCREMENT');
}
Foreign key table
public function up()
{
Schema::create('useraccesses', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id',20);
$table->datetime('accessTime')->nullable();
$table->datetime('quittime')->nullable();
$table->datetime('activeHour')->nullable();
$table->text('token')->nullable();
$table->foreign('user_id')
->references('user_id')->on('users')
->onDelete('cascade');
$table->timestamps();
});
}
Thanks
Foreign keys are not supported for MyISAM tables in MySQL. Use InnoDB tables instead. c.f. https://dev.mysql.com/doc/refman/8.0/en/myisam-storage-engine.html
Try this
for users migration
...
$table->increments('id');
...
for useraccesses migration
...
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')
->references('user_id')->on('users')
->onDelete('cascade');
...

Foreign Key issue with migrations

Im using the package https://github.com/musonza/groups to added group system on my laravel app, but when I want to migrate I get the error:
Migration table created successfully.
Migrating: 2019_05_26_125854_create_groups_tables
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `groupsystem`.`#sql-2678_26` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `groups` add constraint `groups_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
This is my 2 migrations table code:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
And also this is the migration table I get from installing the package:
That I mentioned in the link in the top of this post
This is the first time I try to use this package
And im trying tocreate a group system on my laravel application
Any other suggetions will be welcomed
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGroupsTables extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description')->nullable();
$table->string('short_description')->nullable();
$table->string('image')->nullable();
$table->string('url')->nullable();
$table->integer('user_id')->unsigned();
$table->boolean('private')->unsigned()->default(false);
$table->integer('conversation_id')->unsigned()->nullable();
$table->text('extra_info')->nullable();
$table->text('settings')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('group_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('group_id')
->references('id')
->on('groups')
->onDelete('cascade');
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->string('type');
$table->integer('user_id')->unsigned();
$table->text('extra_info')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('body');
$table->integer('user_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->string('type')->nullable();
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users');
$table->foreign('post_id')
->references('id')
->on('posts');
});
Schema::create('group_post', function (Blueprint $table) {
$table->integer('group_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->timestamps();
$table->foreign('group_id')
->references('id')
->on('groups')
->onDelete('cascade');
$table->foreign('post_id')
->references('id')
->on('posts')
->onDelete('cascade');
});
Schema::create('likes', function (Blueprint $table) {
$table->integer('user_id')->index();
$table->integer('likeable_id')->unsigned();
$table->string('likeable_type');
$table->primary(['user_id', 'likeable_id', 'likeable_type']);
$table->timestamps();
});
Schema::create('reports', function (Blueprint $table) {
$table->integer('user_id')->index();
$table->integer('reportable_id')->unsigned();
$table->string('reportable_type');
$table->primary(['user_id', 'reportable_id', 'reportable_type']);
$table->timestamps();
});
Schema::create('group_request', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->integer('group_id')->unsigned()->index();
$table->timestamps();
$table->foreign('group_id')
->references('id')
->on('groups')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('groups');
Schema::drop('group_user');
Schema::drop('posts');
Schema::drop('comments');
Schema::drop('group_post');
Schema::drop('likes');
Schema::drop('reports');
Schema::drop('group_request');
}
}
If you have used laravel 5.8 then
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
If you have used laravel < 5.8 then
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
I did found the solution it's because of the new Laravel 5.8.3 it comes with unsignedBigInteger(id) instead of increments(id)

Duplication of columns

I trying to create foreign keys for my laravel 5.8 project, but I am getting error:
My migration file
public function up()
{
Schema::table('chatter_discussion', function (Blueprint $table) {
$table->integer('chatter_category_id')->unsigned()->index();
$table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
->onDelete('cascade')
->onUpdate('cascade');
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Schema::table('chatter_post', function (Blueprint $table) {
$table->integer('chatter_discussion_id')->unsigned()->index();
$table->foreign('chatter_discussion_id')->references('id')->on('chatter_discussion')
->onDelete('cascade')
->onUpdate('cascade');
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
You can just define your relationships within your models and forget about setting foreign keys etc.
https://laravel.com/docs/5.8/eloquent-relationships
So in your ChatterDiscussion/ChatterPost models (which you may have to create) you would have a function like:
public function chatter_category()
{
return $this->hasOne('App\ChatterCategory');
}
And in your ChatterCategory (whihch you may also need to create) you would have the inverse:
public function chatter_discussion()
{
return $this->belongsTo('App\ChatterDiscussion');
}
public function chatter_post()
{
return $this->belongsTo('App\ChatterPost');
}
You can then handle the deletes in each of the delete functions within your models.
Either way, your error is that you are creating the same column name twice. Something like this should work (I have not tested this)
Schema::table('chatter_discussion', function (Blueprint $table) {
$table->foreign('chatter_category_id')->unsigned()->index()->references('id')->on('chatter_categories')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
Schema::table('chatter_post', function (Blueprint $table) {
$table->foreign('chatter_discussion_id')->unsigned()->index()->references('id')->on('chatter_discussion')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});

How to foreign key in Laravel 5.3

I use Laravel 5.3 and MySQL
I will to make foreign key on Forums and references on user id
Migration in Users
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('api_token', 60)->unique();
$table->rememberToken();
$table->timestamps();
});
}
Migration in Forums
{
Schema::create('forums', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('thread');
$table->text('deskripsi');
$table->timestamps();
});
Schema::table('forums', function(Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users');
});
}
But I get found error
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`dbkelas`.`forums`, CONSTRAINT `forums_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) (SQL: insert into `forums` (`thread`, `deskripsi`, `updated_at`, `created_at`) values (Matematika Diskrit, Bagaimana ya, 2017-01-14 15:10:18, 2017-01-14 15:10:18))
Do you have any solution?
I think your migration code need to update like:
Schema::create('forums', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->string('thread');
$table->text('deskripsi');
$table->timestamps();
});
Hope this work for you!

Getting error: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in Laravel 5

I have this four tables but I'm getting the above error in Laravel eloquent. can anyone tell what am I missing here? But when I remove the foreign method the migration works fine.
Schema::create('students', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('first_name');
$table->string('last_name');
$table->string('middle_name');
$table->date('birthdate');
$table->integer('degree_id')->unsigned();
$table->index(['id','first_name', 'last_name']);
$table->timestamps();
$table->foreign('degree_id')
->references('id')
->on('degrees');
});
I included unsigned method but still getting the above error.
Schema::create('degrees', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('description');
$table->string('category_id');
$table->integer('duration');
$table->string('sub_code');
$table->timestamps();
$table->foreign('sub_code')
->references('id')
->on('courses');
});
Here's the other two tables.
Schema::create('instructors', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('first_name');
$table->string('middle_name');
$table->string('last_name');
$table->date('birthdate');
$table->index(['id', 'first_name', 'last_name']);
$table->timestamps();
});
And the last table:
Schema::create('courses', function (Blueprint $table) {
$table->string('id')->unique();
$table->string('description');
$table->integer('no_of_units');
$table->string('room_id');
$table->date('schedule');
$table->integer('instructor_id')->unsigned();
$table->timestamps();
$table->foreign('instructor_id')
->references('id')
->on('instructors');
});
The order of your migration files matters. If you're creating the students table first before the degrees table, your foreign key constraint will fail because the degrees table hasn't been created yet.
To solve this, you can move all your foreign key constraints into a migration create_foreign_key_constraints_migration which should run last.

Resources