[PR #8425] Fetching entities with Composite Key Relations #10981

Closed
opened 2026-01-22 16:09:19 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/8425

State: closed
Merged: No


In our database design we are using entities with composite keys, and we are using composite keys relations between entities.

Let say we have the following entities with Primary Keys (PK):

  • Company (PK: company_code)
  • Customer (PK composite: company_code + customer_code)
  • Invoice (PK composite: company_code + invoice_code)

and the following relations in Invoice:

  • to Company (using company_code), not nullable
  • to Customer (using composite key company_code + customer_code), nullable

Now we have company_code (not null) but no customer_code (null).
We want to fetch Invoice entity, so that we get not null Company, and null Customer.

The problem is that loading Invoice with null Customer is not working - we are getting the following exception:

Exception: [Doctrine\Common\Proxy\Exception\OutOfBoundsException] Missing value for primary key code on Doctrine\Tests\Models\CompositeKeyRelations\CustomerClass

This PR provide a fix, to load the Customer in the described case.
The change is pretty simply, and no other tests are affected.

NOTE: Loading Company is not a problem as it is using relation on just one column (not a composite key). The description includes it just for completeness of the example.

**Original Pull Request:** https://github.com/doctrine/orm/pull/8425 **State:** closed **Merged:** No --- In our database design we are using entities with composite keys, and we are using composite keys relations between entities. Let say we have the following entities with Primary Keys (PK): - `Company` (PK: `company_code`) - `Customer` (PK composite: `company_code` + `customer_code`) - `Invoice` (PK composite: `company_code` + `invoice_code`) and the following relations in `Invoice`: - to `Company` (using `company_code`), not nullable - to `Customer` (using composite key `company_code` + `customer_code`), nullable Now we have `company_code` (not null) but no `customer_code` (null). We want to fetch `Invoice` entity, so that we get not null `Company`, and null `Customer`. The problem is that loading `Invoice` with null `Customer` is not working - we are getting the following exception: ``` Exception: [Doctrine\Common\Proxy\Exception\OutOfBoundsException] Missing value for primary key code on Doctrine\Tests\Models\CompositeKeyRelations\CustomerClass ``` This PR provide a fix, to load the `Customer` in the described case. The change is pretty simply, and no other tests are affected. NOTE: Loading `Company` is not a problem as it is using relation on just one column (not a composite key). The description includes it just for completeness of the example.
admin added the pull-request label 2026-01-22 16:09:19 +01:00
admin closed this issue 2026-01-22 16:09:19 +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#10981