【问题标题】:Add foreign Key to Existing Table Laravel 4将外键添加到现有表 Laravel 4
【发布时间】:2013-12-27 13:40:29
【问题描述】:

我无法向现有表添加外键,我的第一次迁移是:

public function up() {
    Schema::create('clases_inventario', function($t) {
                $t->increments('id');
                $t->integer('grupos_inventario_id');
                $t->integer('laboratorio_id');
                $t->string('codigo', 4);
                $t->string('descripcion', 64);
                $t->boolean('estado')->default(true);
                $t->timestamps();
            });
}

我的第二次迁移是添加外键是

public function up() {

        Schema::table('clases_inventario', function($table) {
                    $table->foreign('grupos_inventario_id')->references('id')->on('grupos_inventario');
                });
    }

然后我运行php artisan migration 并输出下一个错误

 [Exception]
 SQLSTATE[HY000]: General error: 1005 Can't create table 'bd_e_soft.#sql-818
 _55' (errno: 150) (SQL: alter table `clases_inventario` add constraint clas
 es_inventario_grupos_inventario_id_foreign foreign key (`grupos_inventario_
 id`) references `grupos_inventario` (`id`)) (Bindings: array (
 ))

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    外键需要无符号: http://laravel.com/docs/schema#foreign-keys

    将远程表的密钥更改为无符号

    public function up() {
    Schema::create('clases_inventario', function($t) {
                $t->increments('id');
                $t->integer('grupos_inventario_id')->unsigned();
                $t->integer('laboratorio_id');
                $t->string('codigo', 4);
                $t->string('descripcion', 64);
                $t->boolean('estado')->default(true);
                $t->timestamps();
            });
    }
    

    【讨论】:

    • 感谢您的帮助,:)
    猜你喜欢
    • 2012-04-19
    • 2020-04-14
    • 2014-02-20
    • 1970-01-01
    • 2021-08-13
    • 2013-07-13
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多