Manual foreign key constraint definition #5213

Closed
opened 2026-01-22 15:01:41 +01:00 by admin · 2 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:41 +01:00
admin closed this issue 2026-01-22 15:01:43 +01:00
Author
Owner

@SenseException commented on GitHub (May 10, 2018):

If this is about an arbitrary join like it is described at the bottom of https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples, this issue can be closed.

@SenseException commented on GitHub (May 10, 2018): If this is about an arbitrary join like it is described at the bottom of https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#dql-select-examples, this issue can be closed.
Author
Owner

@Ocramius commented on GitHub (Aug 23, 2018):

Closing as per @SenseException's comment.

@Ocramius commented on GitHub (Aug 23, 2018): Closing as per @SenseException's comment.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5213