matchish / laravel-scout-elasticsearch

Search among multiple models with ElasticSearch and Laravel Scout
MIT License
702 stars 113 forks source link

[BUG] The attribute [routing] either does not exist or was not retrieved for model [App\Models\Track]. #247

Open defser opened 1 year ago

defser commented 1 year ago

Describe the bug MissingAttributeException error appears when running php artisan scout:import

final class Track extends Model { use HasFactory; use Searchable; use SoftDeletes; }

Importing [App\Models\Track] Create write index 2/5 [⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬➤⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬⚬] 40%

Illuminate\Database\Eloquent\MissingAttributeException

The attribute [routing] either does not exist or was not retrieved for model [App\Models\Track].

at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:472

To Reproduce Steps to reproduce the behavior:

  1. Create a model with final class Track extends Model { use HasFactory; use Searchable; use SoftDeletes; }
  2. Run a command php artisan scout:import
  3. See error The attribute [routing] either does not exist or was not retrieved for model [App\Models\Track].

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:452 Illuminate\Database\Eloquent\Model::throwMissingAttributeExceptionIfApplicable("routing")

2 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2227 Illuminate\Database\Eloquent\Model::getAttribute("routing")

3 vendor/matchish/laravel-scout-elasticsearch/src/ElasticSearch/Params/Bulk.php:47 Illuminate\Database\Eloquent\Model::__get("routing")

Expected behavior model is synced to index

Additional context When adding public null|string $routing = null; to the model the problem is solved but why is this necessary? Also the documentation does not say anything about it.

change possible causing this issue: https://github.com/matchish/laravel-scout-elasticsearch/pull/91/files

Version laravel/framework: v10.8.0 laravel/scout: v10.1.1 matchish/laravel-scout-elasticsearch: v7.2.1

hkulekci commented 1 year ago

Do you have any field as routing in the model? routing is a reserved field in Elasticsearch. Could it be related to that?

hkulekci commented 1 year ago

Ahh, sorry, I saw the PR you shared already now.

matchish commented 1 year ago

I'll try to create a red test next weekend, but I'm pretty sure that you're right and it's because of https://github.com/matchish/laravel-scout-elasticsearch/pull/91/files and some Laravel magic

defser commented 1 year ago

@matchish I now see that one of our configuration causes this issue to happen:

final class AppServiceProvider extends ServiceProvider { public function boot(): void { Model::preventLazyLoading(!$this->app->isProduction()); Model::preventAccessingMissingAttributes(); Model::preventSilentlyDiscardingAttributes(); } }

We use Model::preventAccessingMissingAttributes(); to prevent magical bugs in our application. This exception shows us that the package is trying to access a missing attrubute.

matchish commented 1 year ago

@defser could you try last release? should work fine