Laravel 迁移的多对多
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47327378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Many to Many on Laravel migration
提问by Ying
I have 3 tables to connect each other. Tables name's roles, role_user, and users. I want to make migration on laravel and adding some constraint. and here's what in my role tables migration:
我有 3 张桌子可以相互连接。表名称的角色、role_user 和用户。我想在 laravel 上进行迁移并添加一些约束。这是我的角色表迁移中的内容:
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
and here's my Users table migration:
这是我的用户表迁移:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('active')->default(0);
$table->softDeletes();
$table->rememberToken();
$table->timestamps();
});
and here's my role_user table migration:
这是我的 role_user 表迁移:
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->unique(['role_id', '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');
});
in my Migration order i put roles table on top of users already but i got this kind of errors:
在我的迁移顺序中,我已经将角色表放在用户之上,但是我遇到了这种错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (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)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
回答by Ying
I delete all my table and left just 4 tables :
我删除了我所有的表,只留下了 4 个表:
create_user_table,
create_password_reset,
create_roles_table,
create_role_user
and everything works fine.
一切正常。
Till now I don't understand what makes these errors although I already put the same orders.
直到现在我不明白是什么导致了这些错误,尽管我已经下了相同的订单。
回答by Daniel Cortés
Probably your problem was with the order of creation of migrations.
可能您的问题与迁移的创建顺序有关。
Laravel runs the migrations in order of creation using the timestamp in the file name , because of that, if you created them in the following order:
Laravel 使用文件名中的时间戳按创建顺序运行迁移,因此,如果您按以下顺序创建它们:
- roles in
2018_06_02_023539_create_roles_table
- role_user in
2018_06_02_023800_create_role_user_table
- users in
2018_06_02_023815_create_users_table
- 角色
2018_06_02_023539_create_roles_table
- role_user 在
2018_06_02_023800_create_role_user_table
- 用户在
2018_06_02_023815_create_users_table
The table users
would not exist when referenced in the table role_user
, thus causing an SQL error.
The most simple fix that you could do is renaming the role_user
migration file in this way:
在 table 中users
引用时该表将不存在role_user
,从而导致 SQL 错误。您可以做的最简单的修复role_user
是以这种方式重命名迁移文件:
2018_06_02_023800_create_role_user_table
=> 2018_06_02_023820_create_role_user_table
2018_06_02_023800_create_role_user_table
=> 2018_06_02_023820_create_role_user_table