doujilou3903 2015-09-24 06:45
浏览 57

如何使外键与int类型相同?

I have the following migration file declared in 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');
    }

}

now when i run php artisan migrate

i get an error , because the id in categories is int(10) and the categories_id I.E.

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

is int(11) how do i make both int(10) ?

  • 写回答

2条回答 默认 最新

  • duanlidi1051 2015-09-24 20:19
    关注

    I would guess that your previous migrations were run on a previous version of Laravel. If that's the case, you can simply modify the column type of categories.id to int(11) directly in the DB.

    Otherwise, you could use a Raw Query to add the column using INT(10).

    To confirm the hypothesis that a version change caused this, you could run ALL migrations in an empty DB and see if you have the same issue.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向