Laravel 迁移脚本产生多个主键列
Laravel migration script produces multiple primary key columns
以下迁移函数:
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 的基本代码。)
这是因为integer()
方法的第二个参数是用于自增的步长值。您不能设置整数列的长度,而是使用更适合您需要的列类型。
以下迁移函数:
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 的基本代码。)
这是因为integer()
方法的第二个参数是用于自增的步长值。您不能设置整数列的长度,而是使用更适合您需要的列类型。