Laravel迁移外键问题

时间:2014-06-19 08:46:09

标签: laravel laravel-4

我已经使用类别迁移创建了类别表,然后我尝试使用另一个迁移创建产品表,将产品表中的外键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` ())

2 个答案:

答案 0 :(得分:5)

您可以尝试此操作(注意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');
});

答案 1 :(得分:1)

设置类型时应定义

unsigned(),而不是在设置外键时定义。此外,reference()应为references()

相应的行应该像这样改变:

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