【问题标题】:How to make foreign key same int type ?如何使外键与int类型相同?
【发布时间】:2015-12-21 16:22:03
【问题描述】:

我在 laravel 中声明了以下迁移文件:

<?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($table){
            $table->increments('id');
            $table->integer('category_id');
            $table->string('title');
            $table->text('description');
            $table->decimal('height' , 6 , 2);
            $table->decimal('width' , 6 , 2);
            $table->decimal('length' , 6 , 2);
            $table->string('color');
            $table->string('material');
            $table->timestamps();

        });

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

    }

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

}

现在当我运行php artisan migrate

我收到一个错误,因为类别中的 id 是 int(10) 而 categories_id I.E.

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

int(11) 我如何使两个 int(10) ?

【问题讨论】:

    标签: php laravel-4 database-migration


    【解决方案1】:
    Schema::table('products', function($table)
    {
        $table->integer('id, 10');
    });
    

    【讨论】:

      【解决方案2】:

      我猜你之前的迁移是在之前版本的 Laravel 上运行的。如果是这种情况,您可以直接在DB中将categories.id的列类型修改为int(11)

      否则,您可以使用Raw Query 来添加使用INT(10) 的列。

      要确认版本更改导致此问题的假设,您可以在空数据库中运行所有迁移,看看是否有同样的问题。

      【讨论】:

        猜你喜欢
        • 2018-01-17
        • 2021-07-05
        • 1970-01-01
        • 1970-01-01
        • 2018-09-04
        • 2018-08-18
        • 1970-01-01
        • 2013-07-09
        • 2013-09-20
        相关资源
        最近更新 更多