简体   繁体   English

@Laravel迁移:无法在laravel中添加外键约束

[英]@Laravel Migration: Cannot add foreign key constraint in laravel

I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error: 我试图在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

My migration code is as so: 我的迁移代码是这样的:

fees 费用

<?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 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');
    }    
}   

Any ideas as to what I've done wrong, I want to get this right now, as I've got a lot of tables I need to create eg Users, Students, Leves, etc. Ideally I want to create tables which hold this data with the foreign keys, i..e fees and feetypes. 关于我做错了什么的任何想法,我想立即得到,因为我需要创建很多表,例如,Users,Students,Leves等。理想情况下,我想创建包含此表的表具有外键的数据,即费用和费用类型。

Hope someone can help me to get started. 希望有人可以帮助我入门。

Replace this line: 替换此行:

$table->integer('fee_type_id');

With this: 有了这个:

$table->unsignedInteger('fee_type_id');

In CreateFeesTable Migration. CreateFeesTable迁移中。

Check all data types matches for the referenced data types. 检查所有数据类型是否与引用的数据类型匹配。

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');
    });
}

check level_id in levels , academic_id in academics ,fee_type_id in feetypes are also unsignedInteger or autoincrement and change your table creating script to above one. 检查级别中的level_id,学者中的Academic_id,费用类型中的fee_type_id也是unsignedIntegerautoincrement ,并将您的表创建脚本更改为上面的脚本。

CreateFeesTable.php CreateFeesTable.php

change 更改

$table->integer('fee_type_id');

to

$table->unsignedInteger('fee_type_id');

CreateFeetypesTable CreateFeetypesTable

change 更改

$table->unsignedinteger('fee_type_id');

to

$table->increments('fee_type_id'); or $table->integer('fee_type_id'); $table->integer('fee_type_id');

You must first create an index on the referenced column, ie fee_type_id in feetypes table. 首先必须创建在引用的列,即索引fee_type_idfeetypes表。

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. MySQL需要在外键和引用键上建立索引,以便外键检查可以快速进行,而无需进行表扫描。 In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. 在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列。 Such an index is created on the referencing table automatically if it does not exist. 如果这样的索引不存在,则会在引用表上自动创建。 This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. 如果您创建另一个可用于强制外键约束的索引,则以后可能会静默删除该索引。 index_name, if given, is used as described previously. 如果给定,则使用index_name(如前所述)。

From MySQL Reference Manual MySQL参考手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM