{"record":{"id":"e172515511d006d5","repo":"laravel/framework","slug":"this-database-engine-does-not-support-inserting-wh","errorCode":null,"errorMessage":"This database engine does not support inserting while ignoring errors.","messagePattern":"This database engine does not support inserting while ignoring errors\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Grammars/Grammar.php","lineNumber":1275,"sourceCode":"        $parameters = (new Collection($values))\n            ->map(fn ($record) => '('.$this->parameterize($record).')')\n            ->implode(', ');\n\n        return \"insert into $table ($columns) values $parameters\";\n    }\n\n    /**\n     * Compile an insert ignore statement into SQL.\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  array  $values\n     * @return string\n     *\n     * @throws \\RuntimeException\n     */\n    public function compileInsertOrIgnore(Builder $query, array $values)\n    {\n        throw new RuntimeException('This database engine does not support inserting while ignoring errors.');\n    }\n\n    /**\n     * Compile an insert or ignore statement with a returning clause into SQL.\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  array  $values\n     * @param  array  $returning\n     * @param  array|null  $uniqueBy\n     * @return string\n     *\n     * @throws \\RuntimeException\n     */\n    public function compileInsertOrIgnoreReturning(Builder $query, array $values, array $returning, ?array $uniqueBy)\n    {\n        throw new RuntimeException('This database engine does not support insert or ignore with returning.');\n    }\n","sourceCodeStart":1257,"sourceCodeEnd":1293,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Query/Grammars/Grammar.php#L1257-L1293","documentation":"The base Grammar's compileInsertOrIgnore throws by default. MySqlGrammar, PostgresGrammar, and SQLiteGrammar override it (INSERT IGNORE / ON CONFLICT DO NOTHING / INSERT OR IGNORE). SqlServerGrammar does not override it, so insertOrIgnore() on SQL Server triggers the throw. SQL Server has no native INSERT IGNORE syntax.","triggerScenarios":"Calling ->insertOrIgnore($values) or ->insertOrIgnore([...], returning: [...]) on a SQL Server (sqlsrv) connection.","commonSituations":"Application originally developed on MySQL using insertOrIgnore for bulk imports, then migrated to SQL Server. Test suite running on SQLite works but production on SQL Server fails.","solutions":["Switch to a MySQL, PostgreSQL, or SQLite connection — all three override compileInsertOrIgnore","On SQL Server, use upsert() instead, which maps to MERGE (compileUpsert IS overridden in SqlServerGrammar)","Rewrite as a raw MERGE statement or use a WHERE NOT EXISTS subquery pattern via DB::statement"],"exampleFix":"// before — fails on SQL Server\nDB::table('users')->insertOrIgnore(['email' => 'a@b.com', 'name' => 'Alice']);\n\n// after — upsert works on SQL Server (MERGE under the hood)\nDB::table('users')->upsert(['email' => 'a@b.com', 'name' => 'Alice'], ['email'], ['name']);","handlingStrategy":"type-guard","validationCode":"$driver = DB::connection()->getDriverName();\n\nif ($driver !== 'sqlsrv') {\n    DB::table('users')->insertOrIgnore($values);\n} else {\n    // SQL Server: use upsert or manual NOT EXISTS check\n    DB::table('users')->upsert($values, ['email'], array_keys($values));\n}","typeGuard":"function supportsInsertOrIgnore(): bool {\n    return DB::connection()->getDriverName() !== 'sqlsrv';\n}","tryCatchPattern":"try {\n    DB::table('users')->insertOrIgnore($values);\n} catch (\\RuntimeException $e) {\n    if (str_contains($e->getMessage(), 'inserting while ignoring')) {\n        DB::table('users')->upsert($values, ['email'], array_keys($values));\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Avoid insertOrIgnore when targeting SQL Server — use upsert() instead","Test bulk-insert operations against all target DB engines","Keep a driver-aware data-access layer for insert semantics"],"tags":["insert","ignore","sqlserver","database","upsert"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}