I need to create a one to many relationship between users (created from laravel 7 auth scaffolding) and categories. But when I run migration I get this error: Migrating: 2014_10_12_000000_create_users_table
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table business
.users
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users
add constraint users_category_id_foreign
foreign key (category_id
) references catgories
(id
) on delete cascade)
here is the users schema:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('category_id')->references('id')->on('catgories')
->onDelete('cascade');
});
And this the on of categories:
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('category');
$table->timestamps();
});
I have done some searches and I found out that there are two reasons for such an error, the first is that the categories table migration should be done before the users table one, and the second is that the category id datatype on the users schema and the one of the id on the category must be the same, I verified both of tese conditions and I am still getting the same error. I would be so grateful if someone can tell me what's wrong!
2020_08_22_121823_create_users_table.php
and it will migrate in the correct order – N69Son('catgories')
– N69S