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

ON DUPLICATE KEY UPDATE clause for batch INSERTS #75

Closed
ghost opened this issue Mar 8, 2021 · 5 comments
Closed

ON DUPLICATE KEY UPDATE clause for batch INSERTS #75

ghost opened this issue Mar 8, 2021 · 5 comments

Comments

@ghost
Copy link

ghost commented Mar 8, 2021

In MySQL it is possible to use ON DUPLICATE KEY UPDATE clause in batch inserts, like in the below example:

INSERT INTO `buoy_stations` (`id`, `coords`, `name`, `owner`, `pgm`, `met`, `currents`)
VALUES 
('00922', 'Point(30,-90)','name 1','owner 1','pgm 1','y','y'),
('00923', 'Point(30,-90)','name 2','owner 2','pgm 2','y','y'),
('00924', 'Point(30,-90)','name 3','owner 3','pgm 3','y','y'),
('00925', 'Point(30,-90)','name 4','owner 4','pgm 4','y','y'),
('00926', 'Point(30,-90)','name 5','owner 5','pgm 5','y','y')
ON DUPLICATE KEY
        UPDATE coords=values(coords), name=values(name), owner=values(owner), pgm=values(pgm), met=values(met), currents=values(currents);

However, it appears that it is impossible to perform batch inserts with go-jet's current ON DUPLICATE KEY UPDATE implementation.

@go-jet
Copy link
Owner

go-jet commented Mar 10, 2021

VALUES is currently not supported. There is a workaround using Raw operator:

.ON_DUPLICATE_KEY_UPDATE(
	Link.ID.SET(Link.ID.ADD(Int(11))),
	Link.Name.SET(StringExp(Raw("values(name)"))),
)

@ghost
Copy link
Author

ghost commented Mar 10, 2021

Thanks for the tip @go-jet. It certainly gets the job done, however I find it very tedious to write queries that way. Are there any plans on adding VALUES support?

@go-jet
Copy link
Owner

go-jet commented Mar 11, 2021

Yep, it will be added eventually.

@go-jet go-jet added the enhancement New feature or request label Mar 11, 2021
@ghost
Copy link
Author

ghost commented Jul 18, 2021

Hello @go-jet , I hope you are doing well. This is to remind you that there still is interest in this important (in my opinion) enhancement.

@go-jet go-jet mentioned this issue Sep 30, 2022
@go-jet
Copy link
Owner

go-jet commented Sep 30, 2022

values operator will likely not be supported as both MySQL and MariaDB plan to deprecate it.
MySQL new approach is using insert row alias. Support for mysql insert row alias is included in v2.9.0 release.

Code sample:

stmt := Link.INSERT().
	MODELS([]model.Link{
		{
			ID:          randId,
			URL:         "https://www.postgresqltutorial.com",
			Name:        "PostgreSQL Tutorial",
			Description: nil,
		},
		{
			ID:          randId,
			URL:         "https://www.yahoo.com",
			Name:        "Yahoo",
			Description: testutils.StringPtr("web portal and search engine"),
		},
	}).AS_NEW().
	ON_DUPLICATE_KEY_UPDATE(
		Link.ID.SET(Link.ID.ADD(Int(11))),
		Link.URL.SET(Link.NEW.URL),
		Link.Name.SET(Link.NEW.Name),
		Link.Description.SET(Link.NEW.Description),
	)

@go-jet go-jet added this to the Version 2.9.0 milestone Sep 30, 2022
@go-jet go-jet added missing feature and removed enhancement New feature or request labels Sep 30, 2022
@go-jet go-jet closed this as completed Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant