当我在那时为表创建迁移时,它的through-following错误有问题。
照明\数据库\查询异常:sqlstate[hy000]:常规
错误:1005无法创建表
yourwebs_veridocedu
.
#sql-2c46_8e
(errno:150“外键约束格式不正确”)(sql:
更改表
user_role_mappings
添加约束
user_role_mappings_user_id_foreign
外键(
user_id
)
参考文献
users
(
id
)删除时层叠)
用户角色映射表的迁移:
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');
}
}
表中的FK不工作,无法迁移。