我在Laravel中创建了一个迁移,我需要引用同一个表2次。我解释说:“inventory”表有“responsible”和“created by”字段,它们引用了user表。在某些情况下,负责人和创作者可能是同一人。
这是迁移代码:
Schema::create('inventory', function (Blueprint $table) {
$table->id();
$table->integer('type');
$table->string('state', 50);
$table->timestamps();
$table->foreignId('user_id')->constrained(
table: 'user',
indexName: 'created_inventory_id'
)->cascadeOnUpdate()->restrictOnDelete();
$table->foreignId('user_id')->constrained(
table: 'user',
indexName: 'responsible_inventory_id'
)->cascadeOnUpdate()->restrictOnDelete();
$table->foreignId('area_id')->constrained(
table: 'area',
indexName: 'area_inventory_id'
)->cascadeOnUpdate()->restrictOnDelete();
});
当我运行迁移时,它给了我以下错误:
SQLSTATE[42701]: Duplicate column: 7 ERROR: column "user_id" was specified more than once (Connection: pgsql, SQL: create table "inventory"
我该怎么解决呢?
1条答案
按热度按时间mctunoxg1#
要为“inventory”表中的“inventory manager”和“inventory creator”字段引用同一个“user”表,您应该为它们指定不同的列名。以下是解决此问题的更新迁移代码: