php - Laravel迁移创建具有多个主键的表

标签 php laravel laravel-4

我发出命令:

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

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

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 引用一个客户模型和相应的表。但是,它不应该是 orders 表的主键。

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

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

如何让它发挥作用?

最佳答案

问题是您将函数 ->integer() 与第二个参数一起使用。根据docs第二个参数需要一个 bool 值来设置是否自动增量:bool $autoIncrement = false)。

试试这个:

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

关于php - Laravel迁移创建具有多个主键的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098207/

相关文章:

php - 如何在 mPDF 中设置自定义页面大小?

javascript - 未捕获的语法错误 : Unexpected token< VM3419:1

laravel - 如何使用 Eloquent 删除表中的所有行?

php - mktime() 到 mysql 日期时间列?

laravel - 在LaravelProject中调用未定义函数Xinax\LaravelGettext\bindtextdomain()错误

php - Mysql按最近24小时的日期排序,按id排在前1位

php - 没有从关联表中提取数据 : Laravel

laravel - 是否可以在 Laravel 中暂时禁用事件?

laravel - 如何在获取路由中将默认参数传递给 Laravel Controller

php - 如何在每个页面上验证用户 session