php config_error ai_generated true

Doctrine\ORM\Mapping\MappingException:在实体 'App\Entity\Order' 中检测到重复的列定义 'created_at'

Doctrine\ORM\Mapping\MappingException: Duplicate column definition 'created_at' detected in entity 'App\Entity\Order'

ID: php/symfony-doctrine-mapping-duplicate-column

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2024-05-02首次发现

版本兼容性

版本状态引入弃用备注
Doctrine ORM 2.14 active
Doctrine ORM 2.15 active
Doctrine ORM 3.0 active

根因分析

一个 Doctrine 实体有两个或多个属性映射到同一个数据库列名,通常是由于继承映射冲突或注解/属性中的复制粘贴错误。

English

A Doctrine entity has two or more properties mapped to the same database column name, often due to inheritance mapping conflicts or copy-paste errors in annotations/attributes.

generic

官方文档

https://www.doctrine-project.org/projects/doctrine-orm/en/2.14/reference/inheritance-mapping.html

解决方案

  1. Inspect the entity and its parent classes for duplicate column definitions. Run `php bin/console doctrine:mapping:info` to list all mapped fields. Then check each property's column attribute: e.g., `#[ORM\Column(name: 'created_at')]` should appear only once. Rename one property's column to something else like 'created_at_utc'.
  2. If using inheritance (e.g., MappedSuperclass), ensure the child class does not override a column from the parent. Use `@ORM\AttributeOverride` to explicitly rename columns in the child: `#[ORM\AttributeOverrides([new ORM\AttributeOverride(name: 'createdAt', column: new ORM\Column(name: 'order_created_at'))])]`

无效尝试

常见但无效的做法:

  1. Clearing Doctrine metadata cache with `php bin/console doctrine:cache:clear-metadata` 90% 失败

    The error is in the mapping definition itself, not in a stale cache; clearing cache will not fix duplicate column definitions.

  2. Removing all properties that reference 'created_at' from the entity 50% 失败

    This might remove needed functionality; the correct approach is to rename one of the properties or use a different column name.