Laravel使用多个主键迁移创建表 [英] Laravel migrate creating table with more than one primary key

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

问题描述

我发出了命令:

php artisan generate:scaffold order --fields="customer_id:integer(11), order_date:date, notes:text"

当要迁移时,它以错误结束:

When it gets to migrate, it ends with the error:

General error: 1 table "orders" has more than one primary key (SQL: create table "orders" ("id" integer not null primary key autoincrement, "customer_id" integer not null primary key autoincrement, "order_date" date not null, "notes" text not null))

我试图在模式中使用customer_id引用一个Customer模型和相应的表.但是,它不应该是orders表的主键.

There is a Customer model and corresponding table that I am trying to reference with customer_id in the schema. However, it shouldn't be the primary key for the orders table.

这是阻止迁移运行的up()代码:

Here is the up() code that is preventing migration from running:

    Schema::create('orders', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('customer_id', 11);
        $table->date('order_date');
        $table->text('notes');
        $table->timestamps();
    });

我如何使它工作?

推荐答案

可能的问题可能是:

从此处

问题的根源是:具有外键的列必须与该键具有相同的类型.并且您有不同的类型:INT/UNSIGNED INT

Root of problem is : column with foreign key must be same type as that key. And you have different types: INT/UNSIGNED INT

有关Laravel的文档也提到了这一点: 注意:创建引用递增整数的外键时,请记住始终使外键列为无符号.

The documentation on Laravel mentions this also : Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.

这篇关于Laravel使用多个主键迁移创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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