mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query. #6471
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @augustomurri on GitHub (May 15, 2020).
Main entity Conferimenti.php
Inforifiuti.php
Infocomune.php
I am tryng to execute this query (working on Postgres) but can't get it working with doctrine ORM :(
Querybuilder
I get this error
A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query.
tryed to replace ->GroupBy with but same error
@augustomurri commented on GitHub (May 18, 2020):
UPDATE
Tryed using direct DQL but same identical error
A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query.
@fyrye commented on GitHub (May 23, 2020):
The mapping for your relationships does not appear to be valid.
Without knowing your database table schema, to me it seems like you're incorrectly assigning
conferimenti.idcomuneto two separate table associations. WhereInforifiutishould have aManyToOnerelationship of theInfocomuneentity instead of anColumn(type="integer")data-type. Removing theConferimenti::$idcomuneproperty, as it is indirectly referenced by@ORM\JoinColumn(name="idcomune", referencedColumnName="idcomune").For Example
I suggest you reevaluate the entity mappings.
However you also appear to have general issues with your DQL statements.
You have to keep in mind that DQL is working with entity mappings and NOT the database table column names. Which doctrine processes the entity mappings within the DQL into the specified column name references for use in an SQL query.
For a DQL
WHEREcondition to be valid, you need to specify the entity properties that reference columns, instead of specifying the entity properties that reference entity associations.For Example:
WHERE App\Entity\Infocomune = :id_comuneis not valid, which is what your current statements translate to.Based on your current mapping, the criteria for
WHERE c.idcomune = :id_comuneandGROUP BY c.tiporifiuto, are not correctly referenced, asc.idcomuneis anApp\Entity\Infocomuneentity andc.tiporifiutois anApp\Entity\Inforifiutientity.However, as I mentioned, your association for
Conferimenti::$tiporifiutodoes not appear to be valid.DQL
Additionally the QueryBuilder example is redefining a relationship that is already specified in your Entity mappings, as you do not need to add the JOIN conditions for the associations. You also need to refine the JOIN condition to account for the
c.idcomune = :id_comuneclause.Result (albeit I am not aware if this is valid for PostgreSQL or your database table schema)
SQL
Alternatively you can use the
IDENTITYfunction in the WHERE clause, to retrieve the column references. However,IDENTITYdoes not work for theGROUP BYclause. Requiring you toGROUP BY r.codicerifiutoinstead ofGROUP BY IDENTITY(c.tiporifiuto, 'codicerifiuto').DQL
Results in
SQL
Lastly the DQL statement that references the entity class directly, does require specification of the JOIN criteria, as it is the equivalent to specifying the table name in SQL.
For Example:
DQL
Results in
SQL
Instead you would want to use.
DQL
Results in
SQL
@ihpr commented on GitHub (Jul 5, 2022):
I have reproduced the same issue within a separate repository.
I tried to find a solution in Doctrine documentation but it's quite limited with examples of composite PK usage.
I didn't find a clear answer. Maybe someone like @Ocramius or anybody else can clarify the solution to me?
Thank you in advance.
@fyrye commented on GitHub (Jul 5, 2022):
@iggyster It appears you have an incomplete reference in your DQL query.
As explained in the last section of my prior comment; "Lastly the DQL statement that references the entity class directly, does require specification of the JOIN criteria, as it is the equivalent to specifying the table name in SQL."
Solution
Without knowing more as to why you are manually wiring the relationship in the DQL, instead allow Doctrine to parse the relationships, where the above query would typically look like the following:
SQL
Alternative
Otherwise to manually wire the relationship in the DQL, the full criteria needs to be specified verbosely,
For clarification
IDENTITY(o.user, 'status'), checks thenamevalue from theJoinColumn(referencedColumn="status")definition of yourOrder::$userentity property, to be parsed aso.user_statusin SQL, but sinceOrder::$user_statusis not a property of yourOrderentity it cannot be used directly aso.user_statusin the DQL statement.SQL
You may even be able to use the following DQL to improve query performance.
SQL
@ihpr commented on GitHub (Jul 5, 2022):
@fyrye Thank you for the explanation)
P.S. The issue can be closed.