Multiple composite foreign keys sharing a common column #6968

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

Originally created by @theoaks on GitHub (Apr 27, 2022).

Bug Report

Q A
BC Break yes
Version 2.12.1

Summary

I have a table having multiple composite foreign key references to the same table. All the foreign key references have a single column they share together.

Table1
ID(PK)

TableA:
ColA (PK1, FK[Table1(ID)])
ColB (PK2)

TableB:
ID(PK)
Table1ID(FK[Table1(ID)])
Col1 (FK[TableA(ColA = Table1ID, ColB)])
Col2 (FK[TableA(ColA = Table1ID, ColB)])
Col3 (FK[TableA(ColA = Table1ID, ColB)])

Current behavior

When any of $TableB::$tableA1, TableB::$tableA2 or TableB::$tableA3 is set to null, column (Table1ID on TableB) is set to null while the other columns (Col1, Col2 or Col3 on TableB) retain their values. This breaks the composite foreign key.

How to reproduce

class Table1 {
     #[Id]
     #[Column]
     private ?int $id
}

class TableA {
     #[Id]
    #[ManyToOne(targetEntity: Table1::class)]
    #[JoinColumn(name: 'ColA', referencedColumnName: 'ID')]
     private ?Table1 $table1;
     
     #[Id]
     #[Column]
     private ?string $code;
}

class TableB {
       #[Id]
       #[Column]
       private ?int $id;

       
     #[Id]
    #[ManyToOne(targetEntity: Table1::class)]
    #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ID')]
     private ?Table1 $table1;

       #[Id]
    #[ManyToOne(targetEntity: TableA::class)]
    #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ColA')]
    #[JoinColumn(name: 'Col1', referencedColumnName: 'ColB')]
       private ?TableA $tableA1;

       #[Id]
    #[ManyToOne(targetEntity: TableA::class)]
    #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ColA')]
    #[JoinColumn(name: 'Col2', referencedColumnName: 'ColB')]
       private ?TableA $tableA2;

       #[Id]
    #[ManyToOne(targetEntity: TableA::class)]
    #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ColA')]
    #[JoinColumn(name: 'Col3', referencedColumnName: 'ColB')]
       private ?TableA $tableA3;
}

Expected behavior

Setting any of TableB::$tableA1, TableB::$tableA2, TableB::$tableA3 to null shouldn't affect the shared composite foreign key column

Originally created by @theoaks on GitHub (Apr 27, 2022). ### Bug Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes | Version | 2.12.1 #### Summary I have a table having multiple composite foreign key references to the same table. All the foreign key references have a single column they share together. Table1 **ID(PK)** TableA: **ColA (PK1, FK[Table1(ID)])** **ColB (PK2)** TableB: **ID(PK)** **Table1ID(FK[Table1(ID)])** **Col1 (FK[TableA(ColA = Table1ID, ColB)])** **Col2 (FK[TableA(ColA = Table1ID, ColB)])** **Col3 (FK[TableA(ColA = Table1ID, ColB)])** #### Current behavior When any of $TableB::$tableA1, TableB::$tableA2 or TableB::$tableA3 is set to null, column (Table1ID on TableB) is set to null while the other columns (Col1, Col2 or Col3 on TableB) retain their values. This breaks the composite foreign key. #### How to reproduce ```php class Table1 { #[Id] #[Column] private ?int $id } class TableA { #[Id] #[ManyToOne(targetEntity: Table1::class)] #[JoinColumn(name: 'ColA', referencedColumnName: 'ID')] private ?Table1 $table1; #[Id] #[Column] private ?string $code; } class TableB { #[Id] #[Column] private ?int $id; #[Id] #[ManyToOne(targetEntity: Table1::class)] #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ID')] private ?Table1 $table1; #[Id] #[ManyToOne(targetEntity: TableA::class)] #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ColA')] #[JoinColumn(name: 'Col1', referencedColumnName: 'ColB')] private ?TableA $tableA1; #[Id] #[ManyToOne(targetEntity: TableA::class)] #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ColA')] #[JoinColumn(name: 'Col2', referencedColumnName: 'ColB')] private ?TableA $tableA2; #[Id] #[ManyToOne(targetEntity: TableA::class)] #[JoinColumn(name: 'Table1ID', referencedColumnName: 'ColA')] #[JoinColumn(name: 'Col3', referencedColumnName: 'ColB')] private ?TableA $tableA3; } ``` #### Expected behavior Setting any of TableB::$tableA1, TableB::$tableA2, TableB::$tableA3 to null shouldn't affect the shared composite foreign key column
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6968