【问题标题】:Laravel migration foreign key issueLaravel 迁移外键问题
【发布时间】:2014-08-09 17:50:33
【问题描述】:

我已经使用类别迁移创建了类别表,然后我尝试使用另一个迁移创建产品表,其中产品表中的外键 categories_id 到产品表中的 id。

请在下面找到我的迁移。

Categories migration

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCategoriesTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories',function(Blueprint $table)
        {
            $table->increments('id');

            $table->string('category_name', 255);
            $table->string('category_description', 255);

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('categories');
    }

}

Products migration

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProductsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->reference('id')->on('categories');
            $table->string('product_name');
            $table->text('product_description');
            $table->decimal('product_cost', 10, 2);
            $table->text('product_image');
            $table->boolean('product_availability')->default(1);

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('products');
    }

}

当我在命令行中运行 php artisan migrate 命令时出现以下错误

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MySQL server v
ersion for the right syntax to use near ')' at line 1 (SQL: alter table `pr
oducts` add constraint products_category_id_foreign foreign key (`category_
id`) references `categories` ())

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    你可以试试这个(注意unsignedreferences你用过reference):

    // unsigned() should be used during declaration
    $table->integer('category_id')->unsigned();
    
    // reference() should be references()
    $table->foreign('category_id')->references('id')->on('categories');
    

    更新:

    首先创建products 表,然后添加foreign 键。创建表时删除以下行:

    $table->foreign('category_id')->references('id')->on('categories');
    

    然后使用这个添加外键:

    Schema::table('products', function($table) {
        $table->foreign('category_id')->references('id')->on('categories');
    });
    

    两者应该是不同的:

    Schema::create('products', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('category_id')->unsigned();
        $table->string('product_name');
        // more ...
    });
    
    Schema::table('products', function($table) {
        $table->foreign('category_id')->references('id')->on('categories');
    });
    

    【讨论】:

    • 我已经检查过我尝试过的那个线程,但是我检查我的数据库的方式没有区别,表也被创建并且外键在两个表中都没有签名。问题是因为这个错误我无法迁移未来的迁移
    • 另外,如果 'references()' 拼写错误,也会导致同样的问题 :)
    【解决方案2】:

    unsigned() 应该在设置类型时定义,而不是在设置外键时。另外,reference() 应该是 references()

    相应的行应该这样改:

    $table->integer('category_id')->unsigned();
    $table->foreign('category_id')->references('id')->on('categories');
    

    【讨论】:

    • 即使在分配外键之前添加 unsigned() 后仍然存在相同的错误
    • 你修复了引用吗?
    • 是的,请检查上面批准的答案
    猜你喜欢
    • 2014-03-11
    • 1970-01-01
    • 2015-01-13
    • 2013-05-31
    • 2013-08-27
    • 2020-08-01
    • 2015-03-31
    • 1970-01-01
    • 2020-06-05
    相关资源
    最近更新 更多