Skip to content

Conversation

@UniForceMusic
Copy link
Contributor

@UniForceMusic UniForceMusic commented Dec 4, 2025

The current implementation of ON DUPLICATE KEY and ON CONFLICT does not work for bulk inserts. If someone were to use the updates map to override values, it would override identically for all rows being upserted.

To account for bulk inserts MySQL has `key` = VALUES(`key`) to get the insert value, and Postgres/SQLite has "key" = EXCLUDE."key".

SYNTAX ADDITION:

$insert = $this->factory
    ->insert('users', [
        'username' => 'foo',
        'email' => '[email protected]',
    ])
    ->values('bar', '[email protected]')
    ->values('baz', '[email protected]')
    ->onConflictDoUpdate(
        ['email'],
        [
            'username', // <-- this is new
            'email' => '<user contact info removed>',
        ]
    );

Instead of always requiring a key => value pair, you can also use just the identifier to put a VALUES(key) or EXCLUDED.key in the query.

['username', 'email' => '[email protected]']

Will translate to

"username" = EXCLUDED."username", "email" = ?

This way, the existing syntax still works and nothing is broken

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.

1 participant