Skip to content

scout:import fails when getScoutKeyName() differs from primary key #960

@waszkowski

Description

@waszkowski

Scout Version

10.23.0

Scout Driver

Meilisearch

Laravel Version

12.38.1

PHP Version

8.2

Database Driver & Version

MySQL 8.0 via Laravel Sail

SDK Version

1.16.1

Meilisearch CLI Version

No response

Description

Problem

getScoutKeyName() is currently used in two different contexts:

  1. Field name in search index (Meilisearch, Algolia)
  2. Column name in database queries (SQL)

This causes failures when a model needs different names for each context.

Why this matters

Consider two models indexed together:

Model Database PK Index field
User id (int) id
Invitation uuid (string) id

Both models have id field in Meilisearch, but in database it's id or uuid depending on the model. This is where it breaks - getScoutKeyName() cannot
return both id (for index) and uuid (for database) at the same time.

Affected code

Location Uses getScoutKeyName() for
MeilisearchEngine::update() Index field name ✓
SearchableScope::searchable() chunkById() column ✗
Searchable::queryScoutModelsByIds() whereIn() column ✗
QueueImportCommand::handle() min()/max() column ✗
DatabaseEngine::searchModels() orderBy() column ✗

Steps To Reproduce

  1. Create two models with different primary keys:
// User.php - standard id
class User extends Model
{
    use Searchable;

    protected $primaryKey = 'id';
}

// Invitation.php - uuid primary key
class Invitation extends Model
{
    use Searchable;

    protected $primaryKey = 'uuid';
    protected $keyType = 'string';

    public function getScoutKeyName()
    {
        return 'id'; // Need 'id' in Meilisearch
    }

    public function getScoutKey()
    {
        return $this->uuid;
    }

    public function toSearchableArray()
    {
        return [
            'id' => $this->uuid, // Map uuid to 'id' field
            // ...
        ];
    }
}
  1. Run import:

php artisan scout:import "App\Models\Invitation"

  1. Result:
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'

  The chunkById() uses getScoutKeyName() which returns id, but database column is uuid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions