You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We ran into a use case where it is necessary to extend the pivot query of a model. To do this \Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable::newPivotQuery is overridden as follows;
publicfunctionnewPivotQuery()
{
$query = parent::newPivotQuery();
// add relation's conditions and scopes to the query$this->addDefinedConstraintsToQuery($query);
$related = $this->getRelated();
return$query
->join($related->getTable(), $related->getQualifiedKeyName(), '=', $this->getOtherKey())
->select($this->getTable().'.*');
}
The default pivot query where clauses don't use qualified column names. In some cases this may result in an ambiguous column name error. For example:
# SQLSTATE[23000]: Integrity constraint violation: 1052# Column 'rule_id' in WHERE clause is ambiguousSELECT`cmp_rule`.*FROM`cmp_rule`RIGHT JOIN`reg_rule`ON`reg_rule`.`rule_id`=`cmp_rule`.`rule_id`WHERE`base_id`=1AND`rule_id`IN ( 5 )
After looking into the internals, it appears that InteractsWithPivotTable::newPivotQuery does not use a qualified column names. Other parts of the laravel internals do use qualified column names.
Applying the following changes on 6.x LTS resulted in a working query.
Since you're overriding part of the framework we can't really help you, sorry.
However, I do agree with you that it seems odd that the qualified column names aren't applied. Feel free to attempt a PR. If you do, make sure that all tests pass. Thanks
Yes our use case does indeed override internals. That's why the primary question was focused on why FQN weren't used here. Anyway I opened a PR and all tests are passing. I did have to change two existing tests because they were expecting the column name instead of a FQN. Not sure if this is a problem.
Description:
We ran into a use case where it is necessary to extend the pivot query of a model. To do this
\Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable::newPivotQuery
is overridden as follows;The default pivot query
where
clauses don't use qualified column names. In some cases this may result in an ambiguous column name error. For example:After looking into the internals, it appears that
InteractsWithPivotTable::newPivotQuery
does not use a qualified column names. Other parts of the laravel internals do use qualified column names.Applying the following changes on 6.x LTS resulted in a working query.
Resulting query:
Is there a particular reason that qualified column names are not used in
InteractsWithPivotTable
or can we safely create a PR for this change?A search through recent issues and PRs shows that column names are being replaced with qualified column names in other places too. https://github.com/laravel/framework/search?q=qualified+column&type=issues
The text was updated successfully, but these errors were encountered: