Php 迁移时,表中的FK不工作
当我当时为表创建迁移时,由于以下错误,表出现了问题Php 迁移时,表中的FK不工作,php,laravel,laravel-5,Php,Laravel,Laravel 5,当我当时为表创建迁移时,由于以下错误,表出现了问题 Illumb\Database\QueryException:SQLSTATE[HY000]:常规 错误:1005无法创建表yourwebs\u veridocedu\sql-2c46\u 8e (错误号:150“外键约束格式不正确”)(SQL: 更改表用户角色映射添加约束 user\u-role\u-mappings\u-user\u-id\u-foreign外键(user\u-id) 删除级联上的引用用户(id) userrolemappi
Illumb\Database\QueryException:SQLSTATE[HY000]:常规 错误:1005无法创建表
yourwebs\u veridocedu
\sql-2c46\u 8e
(错误号:150“外键约束格式不正确”)(SQL:
更改表用户角色映射
添加约束
user\u-role\u-mappings\u-user\u-id\u-foreign
外键(user\u-id
)
删除级联上的引用用户
(id
)
userrolemapping表的迁移:
public function up()
{
Schema::create('user_role_mappings', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('group_id');
$table->integer('roleid');
$table->integer('status');
$table->integer('createdby');
$table->integer('modifiedby');
$table->string('publicguid');
$table->string('privateguid');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
});
}
角色表的迁移:
public function up()
{
Schema::create('userroles', function (Blueprint $table) {
$table->increments('id');
$table->string('rolename');
$table->integer('status')->default(1);
$table->integer('createdby')->default(1);
$table->integer('modifiedby')->default(1);
$table->string('publicguid');
$table->string('privateguid');
$table->timestamps();
});
}
用户表迁移:
<?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->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->string('email',150)->unique();
$table->string('password');
$table->integer('roleid')->unsigned();
$table->rememberToken();
/*Common Fields*/
$table->integer('status');
$table->integer('createdby');
$table->integer('modifiedby');
$table->string('publicguid');
$table->string('privateguid');
$table->timestamps();
/*From other table */
$table->foreign('roleid') ->references('id')->on('userroles')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
增量
列未签名,您的外键列需要具有相同的签名。因此,您应该这样更改列:
$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
试试下面给出的东西
$table->integer('user_id')->unsigned()->index();
$table->integer('group_id')->unsigned()->index();
两个表都必须使用InnoDB
引擎,列类型必须匹配:
Schema::create('user_role_mappings', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->unsignedInteger('user_id');
$table->unsignedInteger('group_id');
});
我有一个onDelete俱乐部那又怎样。你已经删除了条款,我明白你的意思了。如果有字符串,请告诉我应该怎么做?Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表
yourwebs\u veridocedu
\sql-2c46\u ba
(错误号:150“外键约束格式不正确”)(SQL:alter tableusers
add constraintusers\u roleid\u foreign
外键(roleid
)引用userroles
(id
)on delete cascade)light\Database\QueryException:SQLSTATE[HY000]:一般错误:1005无法创建表(错误号:150“外键约束格式不正确”)(SQL:alter tableusers
add constraintusers\u roleid\u Foreign
外键(roleid
)在删除级联中引用userroles
(id
)错误发生在用户表迁移上,但您没有在此处发布该代码。这可能是相同的问题…我添加了用户迁移表user\u role\u映射是否使用InnoDB引擎?不,实际上我已经尝试过了。尝试$table->engine='InnoDB';
和$table->unsignedInteger('user\u id'))
@JonasStaudenmeir它能工作!!我对$table->foreign('course\u code')->references('course\u code')->on('scope\u courses')->onDelete('cascade')有问题;$table->unsignedInteger('group\u id'))
请发布组
迁移。我的意思是它的不同表。你看到了吗?是的,但问题是相同的:列类型必须匹配。组。id
是无符号的(我假定),而用户角色映射。group\u id
是有符号的。