Skip to content

Use model primary key for database queries instead of scout key name#982

Draft
JoshSalway wants to merge 4 commits intolaravel:11.xfrom
JoshSalway:fix/scout-import-key-name-clean
Draft

Use model primary key for database queries instead of scout key name#982
JoshSalway wants to merge 4 commits intolaravel:11.xfrom
JoshSalway:fix/scout-import-key-name-clean

Conversation

@JoshSalway
Copy link
Copy Markdown

@JoshSalway JoshSalway commented Mar 22, 2026

Summary

Fixes database query failures when getScoutKeyName() is overridden to return a different field name than the model's actual primary key. Database operations (chunking, ordering, whereIn) were incorrectly using the Scout key name instead of the database primary key.

Fixes #960

Problem

When a model overrides getScoutKeyName() to return a search-engine-specific field name that differs from the database primary key, all database operations break:

class Product extends Model
{
    protected $primaryKey = 'uuid';

    public function getScoutKeyName(): string
    {
        return 'id'; // Meilisearch document ID field
    }
}

// scout:import fails:
// SQLSTATE: Column not found: 1054 Unknown column 'id' in 'order clause'
// Because chunkById() uses getScoutKeyName() ('id') instead of getKeyName() ('uuid')

Solution

Separate the two concerns across all affected files (SearchableScope, Searchable trait, QueueImportCommand, MakeRangeSearchable, DatabaseEngine, CollectionEngine):

  • Search engine operations (indexing, deleting documents, extracting IDs from search results): continue using getScoutKeyName()
  • Database operations (chunking, ordering, whereIn, min/max): now use getKeyName()

Before/After

Before: scout:import with a custom getScoutKeyName() fails with "Column not found" because database queries use the Scout key name.

After: Database queries use the model's actual primary key. Search engine operations continue using the Scout key name. Both work independently.

Test Plan

  • Verify scout:import works with default configuration (getScoutKeyName == getKeyName)
  • Verify scout:import works when getScoutKeyName differs from getKeyName
  • Verify DatabaseEngine and CollectionEngine search results are correctly ordered
  • Run existing test suite

pushpak1300 and others added 2 commits March 18, 2026 09:15
This section was already covered by the skill's description frontmatter and doesn't affect triggering behavior. See laravel/boost#669.
When getScoutKeyName() is overridden to return a different field name
for the search index (e.g., 'id' for Meilisearch) that differs from
the actual database primary key column (e.g., 'uuid'), database
operations like chunkById, orderBy, whereIn, min, max, and whereBetween
fail with "column not found" errors.

This fix separates the two concerns: getScoutKeyName() continues to be
used for search engine operations (indexing, document identification),
while getKeyName() is now used for all database query operations
(chunking, ordering, filtering by IDs).

Affected files:
- SearchableScope: chunkById now uses getKeyName()
- Searchable: makeAllSearchableQuery and queryScoutModelsByIds
- QueueImportCommand: min/max queries
- MakeRangeSearchable: whereBetween query
- DatabaseEngine: orderBy and primary key search
- CollectionEngine: orderBy and result ID plucking

Fixes laravel#960

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

Josh Salway and others added 2 commits March 28, 2026 13:05
Tests were still mocking getScoutKeyName() for database query
operations (queryScoutModelsByIds, SearchableScope chunking) but the
source code now correctly uses getKeyName() for these operations.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants