Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add the ability to use alias when performing upsert via MySQL #42053

Merged
merged 6 commits into from
Apr 20, 2022
Merged

[9.x] Add the ability to use alias when performing upsert via MySQL #42053

merged 6 commits into from
Apr 20, 2022

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