Misleading error message for bi-dir O:O with no mappedBy #6932

Closed
opened 2026-01-22 15:41:40 +01:00 by admin · 0 comments
Owner

Originally created by @kiler129 on GitHub (Feb 22, 2022).

Bug Report

Q A
BC Break no
Version 2.11.1

Summary

Wrong validation message is displayed when an incorrect bidirectional one-to-one mapping is set up. When the owning side is configured correctly and the target side is missing the back reference the ORM suggests adding inverseBy instead of mappedBy.

I may be wrong, but adding such inversedBy creates another error in the mapping, while using mappedBy results in mapping passing the validation.

Current behavior

Mapping
-------

 [FAIL] The entity-class App\Entity\Media mapping is invalid:
 * The field App\Entity\Media#attachment is on the owning side of a bi-directional relationship, but the specified mappedBy association on the target-entity App\Entity\Attachment# does not contain the required 'inversedBy' attribute.

How to reproduce

  1. Configure two entities like so:
#[ORM\Entity]
class Media
{
    //...
    #[ORM\OneToOne(inversedBy: 'media')]
    #[ORM\JoinColumn(name: 'ZTH1', referencedColumnName: 'ZPK')]
    private Attachment $attachment;
}

#[ORM\Entity]
class Attachment
{
    #[ORM\OneToOne]
    #[ORM\JoinColumn(name: 'ZMD0', referencedColumnName: 'ZPK')]
    private ?Media $media;
  1. Run bin/console doctrine:schema:validate

Expected behavior

I believe that the message should suggest adding mappedBy and not inversedBy, as according to the docs, the owning side needs inversedBy while the target side uses mappedBy. In addition it seems like the target entity reference should contain a field which is missing (nothing after #).

With both fixed the message will look like the following:

 [FAIL] The entity-class App\Entity\Media mapping is invalid:
 * The field App\Entity\Media#attachment is on the owning side of a bi-directional relationship, but the specified mappedBy association on the target-entity App\Entity\Attachment#media does not contain the required 'mappedBy' attribute.

See PR https://github.com/doctrine/orm/pull/9537

Originally created by @kiler129 on GitHub (Feb 22, 2022). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | no | Version | 2.11.1 #### Summary Wrong validation message is displayed when an incorrect bidirectional one-to-one mapping is set up. When the owning side is configured correctly and the target side is missing the back reference the ORM suggests adding `inverseBy` instead of `mappedBy`. I may be wrong, but adding such `inversedBy` creates another error in the mapping, while using `mappedBy` results in mapping passing the validation. #### Current behavior ``` Mapping ------- [FAIL] The entity-class App\Entity\Media mapping is invalid: * The field App\Entity\Media#attachment is on the owning side of a bi-directional relationship, but the specified mappedBy association on the target-entity App\Entity\Attachment# does not contain the required 'inversedBy' attribute. ``` #### How to reproduce 1. Configure two entities like so: ```php #[ORM\Entity] class Media { //... #[ORM\OneToOne(inversedBy: 'media')] #[ORM\JoinColumn(name: 'ZTH1', referencedColumnName: 'ZPK')] private Attachment $attachment; } #[ORM\Entity] class Attachment { #[ORM\OneToOne] #[ORM\JoinColumn(name: 'ZMD0', referencedColumnName: 'ZPK')] private ?Media $media; ``` 2. Run `bin/console doctrine:schema:validate` #### Expected behavior I believe that the message should suggest adding `mappedBy` and not `inversedBy`, as [according to the docs](https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/reference/association-mapping.html#one-to-one-bidirectional), the owning side needs `inversedBy` while the target side uses `mappedBy`. In addition it seems like the target entity reference should contain a field which is missing (nothing after `#`). With both fixed the message will look like the following: ``` [FAIL] The entity-class App\Entity\Media mapping is invalid: * The field App\Entity\Media#attachment is on the owning side of a bi-directional relationship, but the specified mappedBy association on the target-entity App\Entity\Attachment#media does not contain the required 'mappedBy' attribute. ``` See PR https://github.com/doctrine/orm/pull/9537
admin closed this issue 2026-01-22 15:41:40 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6932