Manual foreign key constraint definition #5211

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

Originally created by @ghost on GitHub (Aug 5, 2016).

Originally assigned to: @Ocramius on GitHub.

I have a situation where I need to connect two entities with a shared group key, which is an arbitrary value, which is optional on one of the entities, but not on the other. In a manner of speaking I want to connect EntityB to EntityA, but make sure the group key remains identical in both entities for this connection.

In MySQL this can be achieved with a normal foreign key constraint of the two columns, but in Doctrine this is not possible because the group key is not part of the primary key of EntityA.

EntityA:
  id:
    id:
      type: integer
  fields:
    groupKey:
      type: string
      nullable: true
EntityB:
  id:
    id:
      type: integer
  fields:
    groupKey:
      type: string
      nullable: false
  manyToOne:
    entityA:
      targetEntity: EntityA
      joinColumns:
        entity_a_id:
          referencedColumnName: id
          nullable: false
          onDelete: cascade

        # Not possible:
        groupKey:
          referencedColumnName: groupKey
          nullable: false
          onDelete: cascade

Is there some way this could be achieved? If I leave the double column definition in the yaml, then the schema is correct, but the persister breaks. If I remove it, the migrations:diff replaces the constraint in the schema.

Can I somehow add my own manual foreign key constraint definition, so that MySQL has the double constraint, but Doctrine uses this as a single constraint and continues to work?

Thanks in advance

Originally created by @ghost on GitHub (Aug 5, 2016). Originally assigned to: @Ocramius on GitHub. I have a situation where I need to connect two entities with a shared group key, which is an arbitrary value, which is optional on one of the entities, but not on the other. In a manner of speaking I want to connect EntityB to EntityA, but make sure the group key remains identical in both entities for this connection. In MySQL this can be achieved with a normal foreign key constraint of the two columns, but in Doctrine this is not possible because the group key is not part of the primary key of EntityA. ``` yaml EntityA: id: id: type: integer fields: groupKey: type: string nullable: true ``` ``` yaml EntityB: id: id: type: integer fields: groupKey: type: string nullable: false manyToOne: entityA: targetEntity: EntityA joinColumns: entity_a_id: referencedColumnName: id nullable: false onDelete: cascade # Not possible: groupKey: referencedColumnName: groupKey nullable: false onDelete: cascade ``` Is there some way this could be achieved? If I leave the double column definition in the yaml, then the schema is correct, but the persister breaks. If I remove it, the migrations:diff replaces the constraint in the schema. Can I somehow add my own manual foreign key constraint definition, so that MySQL has the double constraint, but Doctrine uses this as a single constraint and continues to work? Thanks in advance
admin added the Question label 2026-01-22 15:01:39 +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#5211