-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eager loading problem #264
Comments
Can you confirm what your controller action looks like? Or is there no controller action because you're using one from this package (without any modifications). The reason I ask is this is all unit tested so if the data is being loaded from the database via this package, it should be working. If you're doing any custom loading and then passing it to a response class, then it's possible there'd be an eager load problem - because the encoding doesn't do any loading, it expects the data to already be loaded. |
yes, i use default controller from package. |
There's not much for me to go on here - the eager loading is tested, so either the tests are missing something or there's something specific about your scenario. I'm a bit unsure where to start. What does the full stack trace of the exception look like? |
[2023-11-09 08:11:28] local.ERROR: Unable to encode compound document. See previous exception for cause of failure. {"userId":1,"exception":"[object] (LogicException(code: 0): Unable to encode compound document. See previous exception for cause of failure. at /app/vendor/laravel-json-api/encoder-neomerx/src/Document.php:201) [previous exception] [object] (Illuminate\Database\LazyLoadingViolationException(code: 0): Attempted to lazy load [metric] on model [App\Models\Inventory\Inventory] but lazy loading is disabled. at /app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:564) |
Hi! So I think I've got some clarity one what this is, it's highly likely to be incredibly difficult to fix. However, good to have it in the backlog. If my suspicions are correct, it is to do with the fact that both relationships return the same model. What's the full |
that's right. the same model is returned. |
Can you provide some example include paths? Need them to confirm whether my suspicion is correct. |
|
I'm going to have to put this on the backburner I'm afraid... this is likely to be highly complex, and I've got too many other things to look at for now. I will circle back to this. If anyone wants to give it a go at solving it, the first step would be to create a test in the package that reproduces the scenario. I'd be interested in a PR that does just that - i.e. adds a failing test without attempting to fix it. |
I have a problem in the following case and with eager loading:
two fields with the same type:
HasOne::make('metric')->type('inventory-metrics'),
HasOne::make('metric-service', 'metricService')->type('inventory-metrics'),
in model:
public function metric(): HasOne
{
return $this->hasOne(Metric::class, 'id', 'metric_id');
}
public function metricService(): HasOne
{
return $this->hasOne(Metric::class, 'id', 'metric_service_id');
}
in
AppServiceProvider
I haveModel::preventLazyLoading()
I include both relations in the query!
include=metric,metric-service
and throw an exception:
Attempted to lazy load [metric] on model
Originally posted by @veneliniliev in #263 (comment)
The text was updated successfully, but these errors were encountered: