SQLSTATE[HY000]: General error: 1005 during a migration in Laravel

空扰寡人 提交于 2020-01-24 13:54:26

问题


I am using Laravel 6. I want to run my migration files but during the migration of my "create_users_table" file appears the following error:

SQLSTATE[HY000]: General error: 1005 Can't create tab
le `thenewmeetingapp`.`#sql-f3c_b8` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table 
`users` add constraint `users_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`))

I think the error is between the table "users" and the table "permissions" (every user must have a permission and every permission could have many users). However the table "users" is related even with the table "meeting_user" that is a joined table with the table "meetings".

users:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('surname');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('username')->unique();
            $table->string('password');
            $table->bigInteger('permission_id')->unsigned();
            $table->enum('is_active', array(0, 1))->default(1);
            $table->rememberToken();
            $table->timestamps();
            $table->foreign('permission_id')->references('id')->on('permissions');

        });

permissions:

Schema::create('permissions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });

meeting_user:

Schema::create('meeting_user', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('meeting_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('meeting_id')->references('id')->on('meetings')->onDelete('cascade');
        });

The migration of the users table is the first migration to be run. However I tried also to run before the migration of the permissions table and after the user's one but nothing changed. The error was the same. Is someone able to help me?


回答1:


You need to have the other table your foreign key on users points to, permissions, created before you can add the key to users. So this particular migration that creates the users table can NOT be first. The other table, permissions, has to exist before you can reference it.

If you can't reorder these migrations you can remove the foreign key part from the users migration. Then create a new migration that alters the users table and adds the foreign key; which would now (via timestamp) run after the permissions table migration.




回答2:


As explained by @lagbox's answer, you cannot create a foreign key to a column in a table that does not yet exist.
So, the approach I propose is that you change the order of the migrations, changing the timestamp of the file name of each migration, so that they are executed in the following order:

  1. permissions
  2. users
  3. meetings,
  4. meeting_user

Note that after changing the file names, you must run composer dump-autoload before running the migrations again.



来源:https://stackoverflow.com/questions/59012922/sqlstatehy000-general-error-1005-during-a-migration-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!