laravel/framework · error · RuntimeException

This database engine does not support inserting while…

Error message

This database engine does not support inserting while ignoring errors.

What it means

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.

Solutions

  1. Switch to a MySQL, PostgreSQL, or SQLite connection — all three override compileInsertOrIgnore
  2. On SQL Server, use upsert() instead, which maps to MERGE (compileUpsert IS overridden in SqlServerGrammar)
  3. Rewrite as a raw MERGE statement or use a WHERE NOT EXISTS subquery pattern via DB::statement

Example fix

// before — fails on SQL Server
DB::table('users')->insertOrIgnore(['email' => 'a@b.com', 'name' => 'Alice']);

// after — upsert works on SQL Server (MERGE under the hood)
DB::table('users')->upsert(['email' => 'a@b.com', 'name' => 'Alice'], ['email'], ['name']);
Defensive patterns

Strategy: type-guard

Validate before calling

$driver = DB::connection()->getDriverName();

if ($driver !== 'sqlsrv') {
    DB::table('users')->insertOrIgnore($values);
} else {
    // SQL Server: use upsert or manual NOT EXISTS check
    DB::table('users')->upsert($values, ['email'], array_keys($values));
}

Type guard

function supportsInsertOrIgnore(): bool {
    return DB::connection()->getDriverName() !== 'sqlsrv';
}

Try / catch

try {
    DB::table('users')->insertOrIgnore($values);
} catch (\RuntimeException $e) {
    if (str_contains($e->getMessage(), 'inserting while ignoring')) {
        DB::table('users')->upsert($values, ['email'], array_keys($values));
    } else {
        throw $e;
    }
}

Prevention

When it happens

Trigger: Calling ->insertOrIgnore($values) or ->insertOrIgnore([...], returning: [...]) on a SQL Server (sqlsrv) connection.

Common situations: 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.

Related errors


AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11). Data as JSON: /api/errors/e172515511d006d5. Report an issue: GitHub.

Appendix: source

Thrown at src/Illuminate/Database/Query/Grammars/Grammar.php:1275

        $parameters = (new Collection($values))
            ->map(fn ($record) => '('.$this->parameterize($record).')')
            ->implode(', ');

        return "insert into $table ($columns) values $parameters";
    }

    /**
     * Compile an insert ignore statement into SQL.
     *
     * @param  \Illuminate\Database\Query\Builder  $query
     * @param  array  $values
     * @return string
     *
     * @throws \RuntimeException
     */
    public function compileInsertOrIgnore(Builder $query, array $values)
    {
        throw new RuntimeException('This database engine does not support inserting while ignoring errors.');
    }

    /**
     * Compile an insert or ignore statement with a returning clause into SQL.
     *
     * @param  \Illuminate\Database\Query\Builder  $query
     * @param  array  $values
     * @param  array  $returning
     * @param  array|null  $uniqueBy
     * @return string
     *
     * @throws \RuntimeException
     */
    public function compileInsertOrIgnoreReturning(Builder $query, array $values, array $returning, ?array $uniqueBy)
    {
        throw new RuntimeException('This database engine does not support insert or ignore with returning.');
    }

View on GitHub (pinned to e0f6eb3518)