Set foreign key ids works on update but not on create? #7534

Open
opened 2026-01-22 15:53:07 +01:00 by admin · 0 comments
Owner

Originally created by @fGuix on GitHub (Jul 10, 2025).

Bug Report

Q A
Version 3.5.0

Summary

Hi,
I have two DB and I migrate some data from one to the other. The problem is when I migrate the relationship Ids.
Ex.g. : my "Activity" table has a relation with a "Dossier" table through "dossierId"

#[ORM\Column(nullable: true)]
private ?int $dossierId = null;

#[ORM\ManyToOne(inversedBy: 'activities')]
private ?Dossier $dossier = null;

You can notice that I explicitly added the column dossierId. I use the related setter during my migration to set the ID, easier than manually redefining all relations (but it might be a non-intended way of doing this).

Current behavior

With the exact same source data:

  • When the activity does not yet exists in the DB, the dossierId (that is set in the entity) is not saved. (operation create ?)
  • When the activity already exists in DB, the dossierId (that is set in the entity) is saved. (operation update ?)

Expected behavior

Either Both operations should save the id or both operations should not save the id ?
I could understand if none of the operations worked and that is a wrong way of doing. But as it works for the update operation, it feels inconsistent with the create operation.

How to reproduce

A manyToOne relationship between two table and the relationship id explicitly declared.
Create an entity with the id specified. -> id not saved
Update an entity with the id specified -> id saved (and thus the relation is set)

Originally created by @fGuix on GitHub (Jul 10, 2025). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |-------------------------------------------- | ------ | Version | 3.5.0 #### Summary Hi, I have two DB and I migrate some data from one to the other. The problem is when I migrate the relationship Ids. Ex.g. : my "Activity" table has a relation with a "Dossier" table through "dossierId" ```php #[ORM\Column(nullable: true)] private ?int $dossierId = null; #[ORM\ManyToOne(inversedBy: 'activities')] private ?Dossier $dossier = null; ``` You can notice that I explicitly added the column dossierId. I use the related setter during my migration to set the ID, easier than manually redefining all relations (but it might be a non-intended way of doing this). #### Current behavior With the exact same source data: - When the activity does not yet exists in the DB, the dossierId (that is set in the entity) is not saved. (operation create ?) - When the activity already exists in DB, the dossierId (that is set in the entity) is saved. (operation update ?) #### Expected behavior Either Both operations should save the id or both operations should not save the id ? I could understand if none of the operations worked and that is a wrong way of doing. But as it works for the update operation, it feels inconsistent with the create operation. #### How to reproduce A manyToOne relationship between two table and the relationship id explicitly declared. Create an entity with the id specified. -> id not saved Update an entity with the id specified -> id saved (and thus the relation is set)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7534