Query with OneToOne JOIN returns single result? #6665

Closed
opened 2026-01-22 15:36:42 +01:00 by admin · 7 comments
Owner

Originally created by @dVVIIb on GitHub (Mar 26, 2021).

I have a query of the form:

$query = $entityManager->createQuery('SELECT a.id FROM App\Entities\EntityA a JOIN a.entityStatus s');

When the mapping is of the form:

@ORM\OneToOne(targetEntity="App\Entities\EntityStatus", mappedBy="entityA")

I get a single result.

However, when the association is OneToMany, I get multiple results for the same data. The only alteration being @ORM\OneToOne -> @ORM\OneToMany

The data seem to be ok, and map on-to-one to distinct ids.

Am I missing something?

I expect a distinct row per each on-to-one mapping.

"doctrine/doctrine-bundle": "^2.2", "doctrine/doctrine-migrations-bundle": "^3.0", "doctrine/orm": "^2.8"

Originally created by @dVVIIb on GitHub (Mar 26, 2021). I have a query of the form: `$query = $entityManager->createQuery('SELECT a.id FROM App\Entities\EntityA a JOIN a.entityStatus s');` When the mapping is of the form: `@ORM\OneToOne(targetEntity="App\Entities\EntityStatus", mappedBy="entityA")` I get a single result. However, when the association is OneToMany, I get multiple results for the same data. The only alteration being `@ORM\OneToOne -> @ORM\OneToMany` The data seem to be ok, and map on-to-one to distinct ids. Am I missing something? I expect a distinct row per each on-to-one mapping. ` "doctrine/doctrine-bundle": "^2.2", "doctrine/doctrine-migrations-bundle": "^3.0", "doctrine/orm": "^2.8" `
admin closed this issue 2026-01-22 15:36:42 +01:00
Author
Owner

@greg0ire commented on GitHub (Mar 27, 2021):

You are selecting an id, and I don't think there is a way for the ORM to let you obtain the status entities for id 42 from id 42. Try selecting just a instead? That way the ORM will return a collection of EntityA objects.

Also, we try to keep Github issues for bug reports and feature requests only. If you have a question, please ask it on Stack Overflow or in one of the channels mentioned at https://www.doctrine-project.org/community/index.html

@greg0ire commented on GitHub (Mar 27, 2021): You are selecting an `id`, and I don't think there is a way for the ORM to let you obtain the status entities for id 42 from id 42. Try selecting just a instead? That way the ORM will return a collection of `EntityA` objects. Also, we try to keep Github issues for bug reports and feature requests only. If you have a question, please ask it on Stack Overflow or in one of the channels mentioned at https://www.doctrine-project.org/community/index.html
Author
Owner

@dVVIIb commented on GitHub (Mar 27, 2021):

greg0ire, Your answer doesn't make any sense. I did post this here because I think this is a bug. I don't understand why you closed my issue without allowing me to respond. StackOverflow is no place for bug reports. And I tried as you suggested and selected a but this did not help. With the JOIN, I still get a single result. Note, that I don't wish to select a. The association IS identified/enforced by the id. Note that the query I provided is a simplified version reduced to demo the issue. The original query had more fields. If this IS a bug, then this issue should remain open. Otherwise, kindly explain why this is not a bug, and how this issue should be addressed...

@dVVIIb commented on GitHub (Mar 27, 2021): greg0ire, Your answer doesn't make any sense. I did post this here because I think this is a bug. I don't understand why you closed my issue without allowing me to respond. StackOverflow is no place for bug reports. And I tried as you suggested and selected a but this did not help. With the JOIN, I still get a single result. Note, that I don't wish to select a. The association IS identified/enforced by the id. Note that the query I provided is a simplified version reduced to demo the issue. The original query had more fields. If this IS a bug, then this issue should remain open. Otherwise, kindly explain why this is not a bug, and how this issue should be addressed...
Author
Owner

@greg0ire commented on GitHub (Mar 27, 2021):

Your answer doesn't make any sense

Watch your tone please.

I don't understand why you closed my issue without allowing me to respond.

You just responded, and are very much allowed to do so.

And I tried as you suggested and selected a but this did not help.

Please give more details, I'm not sure I understood what your issue is: sometimes you complain about having too many results: "I get multiple results for the same data", and now you are complaining about getting a single result: "I still get a single result". It would help if you showed a sample of what you have in your tables, and what output you expect from the DQL query above.

If this IS a bug, then this issue should remain open.

Yes, I will reopen if you convince me that it is a bug, but right now, you have not quoted any part of the docs showing that it is one.

@greg0ire commented on GitHub (Mar 27, 2021): > Your answer doesn't make any sense Watch your tone please. > I don't understand why you closed my issue without allowing me to respond. You just responded, and are very much allowed to do so. > And I tried as you suggested and selected a but this did not help. Please give more details, I'm not sure I understood what your issue is: sometimes you complain about having too many results: "I get multiple results for the same data", and now you are complaining about getting a single result: "I still get a single result". It would help if you showed a sample of what you have in your tables, and what output you expect from the DQL query above. > If this IS a bug, then this issue should remain open. Yes, I will reopen if you convince me that it is a bug, but right now, you have not quoted any part of the docs showing that it is one.
Author
Owner

@dVVIIb commented on GitHub (Mar 27, 2021):

What your tone please.

Sorry, no tone was intended. English is not my first language. Be mindful and considerate of such.

You just responded, and are very much allowed to do so.

I was previously under the impression that once closed, an issue becomes less noticeable to those other than who are already subscribed. Correct me if I'm wrong.

sometimes you complain about having too many results: "I get multiple results for the same data", and now you are
complaining about getting a single result:

I think you're confusing me with someone else. I never made any such complaint previously, anywhere. I'm only explaining the issue that I have encountered, which is that a single result is returned, when multiple were expected. Like I mentioned, I have multiple distinct values in my tables, not duplicates. Let me try to show a sample set of data:

The additional columns below are probably irrelevant, but I'm including them for sake of completeness.

Table: EntityA
Id PId AId CTId
1 1 3 1
4 4 3 1
5 5 3 1
6 6 3 1

Table: entityStatus
Id CId CreatedAt UpdatedAt CreatedBy UpdatedBy CSId
1 4 '2021-03-25 14:41:21' '2021-03-25 14:41:21' 5 5 1
2 5 '2021-03-25 14:44:47' '2021-03-25 14:44:47' 5 5 1
3 6 '2021-03-25 14:48:30' '2021-03-25 14:48:30' 5 5 1

Here, CId maps to Id of EntityA. I have specified the correct columns using @ORM\JoinColumn. To simplify the issue, I'm only using a single JOIN (The original query had more). Once joined, the output should be as follows, if selecting EntityA.Id, entityStatus.Id:

4 1
5 2
6 3

However, the join only returns a single row.

1 1

Is this the expected behavior? If so, how should I get my intended behavior?

And again, no tone is implied or intended. I'm just trying to explain my issue. If you need more info, just ask, and I'll try to clarify...

@dVVIIb commented on GitHub (Mar 27, 2021): > What your tone please. Sorry, no tone was intended. English is not my first language. Be mindful and considerate of such. > You just responded, and are very much allowed to do so. I was previously under the impression that once closed, an issue becomes less noticeable to those other than who are already subscribed. Correct me if I'm wrong. > sometimes you complain about having too many results: "I get multiple results for the same data", and now you are complaining about getting a single result: I think you're confusing me with someone else. I never made any such complaint previously, anywhere. I'm only explaining the issue that I have encountered, which is that a single result is returned, when multiple were expected. Like I mentioned, I have multiple distinct values in my tables, not duplicates. Let me try to show a sample set of data: The additional columns below are probably irrelevant, but I'm including them for sake of completeness. Table: EntityA `Id PId AId CTId` `1 1 3 1` `4 4 3 1` `5 5 3 1` `6 6 3 1` Table: entityStatus `Id CId CreatedAt UpdatedAt CreatedBy UpdatedBy CSId` `1 4 '2021-03-25 14:41:21' '2021-03-25 14:41:21' 5 5 1` `2 5 '2021-03-25 14:44:47' '2021-03-25 14:44:47' 5 5 1` `3 6 '2021-03-25 14:48:30' '2021-03-25 14:48:30' 5 5 1` Here, CId maps to Id of EntityA. I have specified the correct columns using @ORM\JoinColumn. To simplify the issue, I'm only using a single JOIN (The original query had more). Once joined, the output should be as follows, if selecting EntityA.Id, entityStatus.Id: `4 1` `5 2` `6 3` However, the join only returns a single row. `1 1` Is this the expected behavior? If so, how should I get my intended behavior? And again, no tone is implied or intended. I'm just trying to explain my issue. If you need more info, just ask, and I'll try to clarify...
Author
Owner

@greg0ire commented on GitHub (Mar 27, 2021):

I was previously under the impression that once closed, an issue becomes less noticeable to those other than who are already subscribed. Correct me if I'm wrong.

It does become less noticeable, but everyone watching this repository still gets a notification every time we post something here.

I have specified the correct columns using @Orm\JoinColumn.

You can make sure that is indeed correct by running https://github.com/doctrine/orm/blob/2.8.x/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php. If you use Symfony, that would be bin/console doctrine:schema:validate

However, the join only returns a single row.
1 1

So, it looks as if the join is in fact not performed on the correct columns right? It's as if entityStatus.Id was used instead of entityStatus.CId… what is the SQL query being performed? Do you know how to check that?

Also, don't worry about your english, it's perfect, especially in your last message.

@greg0ire commented on GitHub (Mar 27, 2021): > I was previously under the impression that once closed, an issue becomes less noticeable to those other than who are already subscribed. Correct me if I'm wrong. It does become less noticeable, but everyone watching this repository still gets a notification every time we post something here. > I have specified the correct columns using @Orm\JoinColumn. You can make sure that is indeed correct by running https://github.com/doctrine/orm/blob/2.8.x/lib/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php. If you use Symfony, that would be `bin/console doctrine:schema:validate` > However, the join only returns a single row. > `1 1` So, it looks as if the join is in fact not performed on the correct columns right? It's as if `entityStatus.Id` was used instead of `entityStatus.CId`… what is the SQL query being performed? Do you know how to check that? Also, don't worry about your english, it's perfect, especially in your last message.
Author
Owner

@dVVIIb commented on GitHub (Mar 27, 2021):

Ah, I see now. Thanks for the clarification. It looks like referencedColumnName in JoinColumn has to be specified. The default didn't work for my schema (I had already specified the column on the opposite side, but missed this one.). Dumping the SQL using $queryBuilder->getSQL() helped. Sorry for posting a false issue, but without your comments, the issue would have been a bit difficult to diagnose. I expect everything will work fine now, and maybe this issue will help someone with a similar issue.

Thank you.

@dVVIIb commented on GitHub (Mar 27, 2021): Ah, I see now. Thanks for the clarification. It looks like referencedColumnName in JoinColumn has to be specified. The default didn't work for my schema (I had already specified the column on the opposite side, but missed this one.). Dumping the SQL using $queryBuilder->getSQL() helped. Sorry for posting a false issue, but without your comments, the issue would have been a bit difficult to diagnose. I expect everything will work fine now, and maybe this issue will help someone with a similar issue. Thank you.
Author
Owner

@greg0ire commented on GitHub (Mar 27, 2021):

Sorry for posting a false issue, but without your comments, the issue would have been a bit difficult to diagnose.

You're saying this wasn't a bug after all? What a surprise!

Seriously, there were signs that it was not going to be a bug: doctrine/orm is more than 10 years old. Big bugs have already been caught a long time ago… I know that it's easier for you to get answers from maintainers directly, but can you imagine if everybody did that?

Next time, when in doubt, please please go to Stack Overflow. 🙏 Stack Overflow is not super forgiving, but it will teach you to write clear questions, with examples from the start, and maybe you will even get reputation points in the process. And the people that want to help you, that want to offer support, they are there (or on Slack). Here, what we try to do is take care of actual, legitimate bugs.

maybe this issue will help someone with a similar issue.

That's not wrong, but you know what? That works great with Stack Overflow too.

@greg0ire commented on GitHub (Mar 27, 2021): > Sorry for posting a false issue, but without your comments, the issue would have been a bit difficult to diagnose. You're saying this wasn't a bug after all? What a surprise! Seriously, there were signs that it was not going to be a bug: `doctrine/orm` is more than 10 years old. Big bugs have already been caught a long time ago… I know that it's easier _for you_ to get answers from maintainers directly, but can you imagine if everybody did that? Next time, when in doubt, please please go to Stack Overflow. :pray: Stack Overflow is not super forgiving, but it will teach you to write clear questions, with examples from the start, and maybe you will even get reputation points in the process. And the people that _want_ to help you, that _want_ to offer support, they are there (or on Slack). Here, what we try to do is take care of actual, legitimate bugs. > maybe this issue will help someone with a similar issue. That's not wrong, but you know what? That works great with Stack Overflow too.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6665