【问题标题】:Laravel migration script produces multiple primary key columnsLaravel 迁移脚本生成多个主键列
【发布时间】:2018-03-06 15:46:00
【问题描述】:

如下迁移函数:

public function up()
{
    Schema::create('translations', function (Blueprint $table) {
        $table->increments('id');

        $table->string('table_name', 32);
        $table->string('column_name', 32);
        $table->integer('foreign_key', 11)->unsigned();
        $table->string('locale', 11);

        $table->text('value');

        $table->unique(['table_name', 'column_name', 'foreign_key', 'locale']);

        $table->timestamps();
    });
}

正在生成以下 SQL 查询:

create table `translations` (
  `id` int unsigned not null auto_increment primary key, 
  `table_name` varchar(32) not null, 
  `column_name` varchar(32) not null, 
  `foreign_key` int unsigned not null auto_increment primary key, 
  `locale` varchar(11) not null, 
  `value` text not null, 
  `created_at` timestamp null, 
  `updated_at` timestamp null
) default character set utf8mb4 collate utf8mb4_unicode_ci engine = InnoDB ROW_FORMAT=DYNAMIC

注意foreign_key 字段上的附加auto_increment primary key。这就是问题。如何更改迁移脚本,使其不会使 foreign_key 成为第二个 auto_increment primary key 列?

(如果这看起来很熟悉,这是来自Voyager 的基本代码。)

【问题讨论】:

    标签: laravel laravel-5 laravel-5.4


    【解决方案1】:

    这是因为integer() 方法的第二个参数是用于自动增量的步长值。您不能设置整数列的长度,而是使用更适合您需要的列类型。

    https://laravel.com/docs/5.4/migrations#columns

    【讨论】:

      猜你喜欢
      • 2018-12-27
      • 2020-05-10
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      相关资源
      最近更新 更多