{"record":{"id":"766d95b207a3032d","repo":"laravel/framework","slug":"index-name-contains-invalid-characters-766d95","errorCode":null,"errorMessage":"Index name contains invalid characters.","messagePattern":"Index name contains invalid characters\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php","lineNumber":126,"sourceCode":"    /**\n     * Compile the index hints for the query.\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  \\Illuminate\\Database\\Query\\IndexHint  $indexHint\n     * @return string\n     *\n     * @throws \\InvalidArgumentException\n     */\n    protected function compileIndexHint(Builder $query, $indexHint)\n    {\n        if ($indexHint->type !== 'force') {\n            return '';\n        }\n\n        $index = $indexHint->index;\n\n        if (! preg_match('/^[a-zA-Z0-9_$]+$/', $index)) {\n            throw new InvalidArgumentException('Index name contains invalid characters.');\n        }\n\n        return \"with (index([{$index}]))\";\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  array  $where\n     * @return string\n     */\n    protected function whereBitwise(Builder $query, $where)\n    {\n        $value = $this->parameter($where['value']);\n\n        $operator = str_replace('?', '??', $where['operator']);\n","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php#L108-L144","documentation":"SqlServerGrammar's compileIndexHint only processes 'force' type hints (SQL Server's WITH (INDEX(...)) clause). It validates the index name against /^[a-zA-Z0-9_$]+$/ and throws if the name contains characters outside that set. 'hint' and 'ignore' types are silently ignored (return empty string).","triggerScenarios":"Calling ->forceIndex('bad name') or ->from('table', indexHint with type 'force' and invalid name) on a SQL Server (sqlsrv) connection. Index names with spaces, hyphens, dots, or other special characters trigger the throw.","commonSituations":"Using forceIndex in a multi-database application where the index name is valid on MySQL but rejected by SQL Server's stricter validation. Passing schema-qualified index names with dots.","solutions":["Rename the index in the migration to use only alphanumeric, underscore, and dollar-sign characters","Remove invalid characters from the index name string","If the index truly requires special characters, use a raw SQL hint via fromRaw() instead of the index hint API"],"exampleFix":"// before — dot in index name throws on SQL Server\nDB::table('orders')->forceIndex('dbo.order_idx')->get();\n\n// after — valid name without dots\nDB::table('orders')->forceIndex('order_idx')->get();","handlingStrategy":"validation","validationCode":"$indexName = 'order_idx';\nif (preg_match('/^[a-zA-Z0-9_$]+$/', $indexName)) {\n    DB::table('orders')->forceIndex($indexName)->get();\n} else {\n    $sanitized = preg_replace('/[^a-zA-Z0-9_$]/', '_', $indexName);\n    DB::table('orders')->forceIndex($sanitized)->get();\n}","typeGuard":"function isValidIndexName(string $name): bool {\n    return (bool) preg_match('/^[a-zA-Z0-9_$]+$/', $name);\n}","tryCatchPattern":"try {\n    DB::table('orders')->forceIndex($indexName)->get();\n} catch (\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'invalid characters')) {\n        $sanitized = preg_replace('/[^a-zA-Z0-9_$]/', '_', $indexName);\n        DB::table('orders')->forceIndex($sanitized)->get();\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Use only alphanumeric and underscore characters in index names on SQL Server","On SQL Server, note that only force-type hints produce SQL (hint/ignore are silently dropped)","Avoid schema-qualified names (schema.index) in forceIndex calls — use bare names"],"tags":["index","sqlserver","validation","grammar"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}