staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference #77

Closed vaawebdev closed 4 years ago

vaawebdev commented 4 years ago

I use Laravel 7 Route implicit bindings (https://laravel.com/docs/7.x/routing#implicit-binding) with custom keys for my hasManyDeep relation. Model relation method code public function forms() { return $this->hasManyDeep( Form::class, [ CheckupService::class, Service::class, FormService::class, ] ); }

Route code Route::name('store') ->post('{checkup:id}/{form:code}', 'UserCheckupReportController@store') ->where(['form' => '[A-Z]+']);

This way Laravel guesses that the Checkup has relation forms and searches form with the "code" column.

As soon as the query for the hasManyDeep has the "code" column with forms prefix like this: "forms.code" I get the error

SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference \"code\" is ambiguous

I user laravel v7.6.2 and postgres alpine

staudenmeir commented 4 years ago

This is a Laravel issue, please see https://github.com/laravel/framework/issues/32546.

vaawebdev commented 4 years ago

@staudenmeir could you integrate the query to select main relation model without prefix?

staudenmeir commented 4 years ago

That would be a breaking change.

taylorotwell commented 4 years ago

Attempted fix here: https://github.com/laravel/framework/commit/8d69454575267840643289b8de27d615cfe4bb62

staudenmeir commented 4 years ago

Thanks @taylorotwell.

@vaawebdev The commit also fixes this issue and will be available in the next Laravel release.

vaawebdev commented 4 years ago

@taylorotwell @staudenmeir thanks a lot for the update, appreciated!