Skip to content

Conversation

paulandroshchuk
Copy link
Contributor

@paulandroshchuk paulandroshchuk commented Apr 19, 2022

Why?

Laravel's upsert default behaviour uses the values() function that is deprecated and MySQL >= 8.0.20 will throw a warning when using the values() function in upsert:

Beginning with MySQL 8.0.20, an INSERT ... SELECT ... ON DUPLICATE KEY UPDATE statement that uses VALUES() in the UPDATE clause, like this one, throws a warning:

Reference


Expected behaviour

MySQL >= 8.0.20 does not throw any warnings when using Model::upsert().


How?

Update the behaviour of MySqlGrammar@compileUpsert to use the alias (instead of values) in the upsert query when it is configured in the new database.{mysql-database}.use_upsert_alias config value.


Why can't we update it to always use aliases?

The alias in upsert is only supported beginning with MySQL 8.0.19, so it won't work with earlier versions:

Beginning with MySQL 8.0.19, it is possible to use an alias for the row, with, optionally, one or more of its columns to be inserted, following the VALUES or SET clause, and preceded by the AS keyword. Using the row alias new, the statement shown previously using VALUES() to access the new column values can be written in the form shown here:

Reference


As the other option, a fourth parameter (instead of adding a new config values) (bool $useAlias = false) can be added to the method to make upsert use the alias.

@paulandroshchuk paulandroshchuk changed the title 9.x Add the ability to use alias when performing upsert via MySQL [9.x] Add the ability to use alias when performing upsert via MySQL Apr 20, 2022
@taylorotwell taylorotwell merged commit b28c9e1 into laravel:9.x Apr 20, 2022
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.

2 participants