[英]@Laravel Migration: Cannot add foreign key constraint in laravel
我试图在Laravel中创建外键,但是当我使用artisan迁移表时,抛出以下错误:
λ php artisan migrate
Migration table created successfully.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `fees` add constraint `fee
s_fee_type_id_foreign` foreign key (`fee_type_id`) references `feetypes` (`fee_type_id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
我的迁移代码是这样的:
费用
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFeesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('fees', function (Blueprint $table) {
$table->increments('fee_id');
$table->integer('academic_id')->unsigned();
$table->integer('level_id')->unsigned();
$table->integer('fee_type_id');
$table->string('fee_heading', 100)->nullable();
$table->float('amount', 8, 2);
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('fee_type_id')->references('fee_type_id')->on('feetypes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('fees');
}
}
fesstypes
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFeetypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('feetypes', function (Blueprint $table) {
$table->unsignedinteger('fee_type_id');
$table->string('fee_type', 100);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('feetypes');
}
}
关于我做错了什么的任何想法,我想立即得到,因为我需要创建很多表,例如,Users,Students,Leves等。理想情况下,我想创建包含此表的表具有外键的数据,即费用和费用类型。
希望有人可以帮助我入门。
替换此行:
$table->integer('fee_type_id');
有了这个:
$table->unsignedInteger('fee_type_id');
在CreateFeesTable
迁移中。
检查所有数据类型是否与引用的数据类型匹配。
public function up()
{
Schema::create('fees', function (Blueprint $table) {
$table->increments('fee_id');
$table->unsignedInteger('academic_id');
$table->unsignedInteger('level_id');
$table->unsignedInteger('fee_type_id');
$table->string('fee_heading', 100)->nullable();
$table->float('amount', 8, 2);
$table->foreign('academic_id')->references('academic_id')->on('academics');
$table->foreign('level_id')->references('level_id')->on('levels');
$table->foreign('fee_type_id')->references('fee_type_id')->on('feetypes');
});
}
检查级别中的level_id,学者中的Academic_id,费用类型中的fee_type_id也是unsignedInteger或autoincrement ,并将您的表创建脚本更改为上面的脚本。
CreateFeesTable.php
更改
$table->integer('fee_type_id');
至
$table->unsignedInteger('fee_type_id');
CreateFeetypesTable
更改
$table->unsignedinteger('fee_type_id');
至
$table->increments('fee_type_id');
或$table->integer('fee_type_id');
首先必须创建在引用的列,即索引fee_type_id
在feetypes
表。
MySQL需要在外键和引用键上建立索引,以便外键检查可以快速进行,而无需进行表扫描。 在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列。 如果这样的索引不存在,则会在引用表上自动创建。 如果您创建另一个可用于强制外键约束的索引,则以后可能会静默删除该索引。 如果给定,则使用index_name(如前所述)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.