【问题标题】:Laravel Incorrect table definition; there can be only one auto column and it must be defined as a keyLaravel 表定义不正确;只能有一个自动列,并且必须将其定义为键
【发布时间】:2016-09-13 13:09:38
【问题描述】:

我有一个有 ID 和 Slug 的表。我希望 Slug 和 ID 一样增加,但是当我尝试这个时

$table->increments('slug')->nullable();

我收到一条错误消息,提示只能有一个自动列。有没有解决的办法?我的表结构如下

Schema::create('catalog_products', function ($table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->boolean('is_featured')->nullable();
            $table->string('title')->nullable();
            $table->increments('slug')->nullable();
            $table->text('description')->nullable();
            $table->timestamps();
        });

【问题讨论】:

  • 没有。我们无法定义两个自动列。
  • “我希望 Slug 和 ID 一样增加”是什么意思?
  • 我将如何创建 slug 列以使其递增?
  • 随着 ID 的增加,我希望 slug 字段也增加
  • 不,这是 MySQL 的 innodb 实现的限制。

标签: mysql laravel laravel-5 database-schema


【解决方案1】:

你不能有两个增量,但你可以有一个整数 slug,每次添加一行时,你手动增加你的 slug。

【讨论】:

    猜你喜欢
    • 2020-01-01
    • 2021-07-21
    • 2015-04-05
    • 2013-07-12
    • 1970-01-01
    • 2017-05-31
    • 2013-12-17
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多