SQLSTATE [HY000]:一般错误:在Laravel迁移期间出现1005 [英] SQLSTATE[HY000]: General error: 1005 during a migration in Laravel

查看:152
本文介绍了SQLSTATE [HY000]:一般错误:在Laravel迁移期间出现1005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel6。我想运行迁移文件,但是在迁移 create_users_table文件时出现以下错误:

  SQLSTATE [HY000]:常规错误:1005无法创建标签
newmeetingapp。#sql-f3c_b8(错误号:150外键约束格式不正确)(SQL: alter table
ʻusers`添加约束ʻusers_permission_id_foreign`外键(`permission_id`)引用`permissions`(ʻid`))

我认为错误是在表 users和表 permissions之间(每个用户必须有一个许可,每个许可可以有许多用户)。
但是,表 users与表 meeting_user相关,表 meeting_user是与表 meetings相连的表。 :

  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->时间戳();
$ table-> foreign('permission_id')-> references('id')-> on('permissions');

});

权限:

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

会议用户:

  Schema :: create('meeting_user',函数(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' );
});

users表的迁移是第一次运行。但是,我也尝试在权限表迁移之前和用户的权限表迁移之后运行,但没有任何变化。错误是相同的。
有人可以帮助我吗?

解决方案

您需要在<$ c上有另一个表您的外键$ c>用户指向权限,该权限是在将密钥添加到用户。因此,不能首先创建创建 users 表的特定迁移。另一个表 permissions ,必须存在,您才能引用它。



如果无法重新排序这些表迁移,您可以从 users 迁移中删除外键部分。然后创建一个新的迁移,以更改 users 表并添加外键;现在,(通过时间戳记)将在权限表迁移之后运行。


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?

解决方案

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.

这篇关于SQLSTATE [HY000]:一般错误:在Laravel迁移期间出现1005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆