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

MariaDB better support #62

Closed
celogeek opened this issue Jan 16, 2022 · 1 comment
Closed

MariaDB better support #62

celogeek opened this issue Jan 16, 2022 · 1 comment
Assignees

Comments

@celogeek
Copy link

celogeek commented Jan 16, 2022

Describe the feature

MariaDB support more feature than MySQL (old version at least).

The one I would love to have is the support of RETURNING clause on the Create operation.

I see a detection of MariaDB, so why not allow additional features there?

I'm trying to create or get a row while inserting. The goal is to handle properly concurrent insert than try to create the same data.

So What I do is something like:

	if err = s.DB.Transaction(func(tx *gorm.DB) (err error) {
		if err = tx.Clauses(clause.OnConflict{DoNothing: true}).Create(u).Error; err != nil {
			return
		}

		if u.ID == 0 {
			if err = tx.First(u, "sha1 = ?", u.Sha1).Error; err != nil {
				return
			}
		}

Motivation

I've got 3 tables to update. I need the ID of the first one to create the entry in the over tables. And 2 parallel request can run the same code. I don't want to end up with a duplicate error.

The create SQL should be:

INSERT INTO T (C1, C2) VALUES(V2, V2) ON DUPLICATE KEY UPDATE id=id RETURNING *

Or ID by default. That could be overwritten.

@celogeek
Copy link
Author

I endup using Raw mode:

func (u *URL) Create(tx *gorm.DB) error {
	return tx.Raw(`
	INSERT INTO urls (
		url,
		sha1,
		created_at
	) VALUES(
		?,
		?,
		NOW()
	) ON DUPLICATE KEY UPDATE id=id
	RETURNING *
	`, &u.Url, u.CheckSumURL()).Scan(u).Error
}

May be I can build the SQL with the clause builder instead and add the RETURNING * on the SQL.

@jinzhu jinzhu closed this as completed in ecb6c6a Oct 8, 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

No branches or pull requests

2 participants