php config_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2024-05-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Doctrine ORM 2.14 active
Doctrine ORM 2.15 active
Doctrine ORM 3.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 90% success 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'.
    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. 80% success 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'))])]`
    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. 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'))])]`

Dead Ends

Common approaches that don't work:

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

    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% fail

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