Laravel 迁移外键问题 [英] Laravel migration foreign key issue

查看:17
本文介绍了Laravel 迁移外键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created categories table using categories migration and then i am trying to create products table using another migration with foreign key categories_id in products table to id in products table.

请在下面找到我的迁移.

Please find my migrations below.

Categories migration

<?php

use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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 IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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 命令时出现以下错误

I am getting this below error when i ran php artisan migrate command in command line

[IlluminateDatabaseQueryException]
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` ())

推荐答案

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

You may try this (Notice unsigned and references you have used 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 键.创建表时删除以下行:

Update:

At first create the products table then add foreign key. Remove the following line when creating the table:

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

然后使用这个添加外键:

Then add foreign key using this:

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

这篇关于Laravel 迁移外键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆