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

why invoke "SHOW VARIABLES LIKE 'auto_increment_increment'" after repo.InsertAll? #304

Closed
hxgdzyuyi opened this issue Jul 6, 2022 · 12 comments

Comments

@hxgdzyuyi
Copy link

step 1

	if err = repo.InsertAll(ctx, &beInsertedAnswers); err != nil {
		return
	}

step 2

2022/07/06 18:19:17 [duration: 1.442817ms op: adapter-exec] INSERT INTO `answers` (`answer_text`,`attempt_id`,....) VALUES (?,?,?,?,?,?,?);
2022/07/06 18:19:17 [duration: 1.398397ms op: adapter-query] SHOW VARIABLES LIKE 'auto_increment_increment';

why invoke "SHOW VARIABLES LIKE 'auto_increment_increment'" after repo.InsertAll?

@Fs02
Copy link
Member

Fs02 commented Jul 6, 2022

because MySQL database driver only returns the last inserted id, and (depending on config), inserted id won't always incremented by 1, therefore we need to know the increment setting to populate the id for the rest of inserted records

@hxgdzyuyi
Copy link
Author

how can i close this feature ?

@Fs02
Copy link
Member

Fs02 commented Jul 7, 2022

do you mean close this issue? or stop using "SHOW VARIABLES LIKE 'auto_increment_increment'" query?

@hxgdzyuyi
Copy link
Author

stop using "SHOW VARIABLES LIKE 'auto_increment_increment'" query?

@hxgdzyuyi
Copy link
Author

stop using "SHOW VARIABLES LIKE 'auto_increment_increment'" query

@Fs02
Copy link
Member

Fs02 commented Jul 7, 2022

it's not supported at the moment, please open a PR to allow specifying custom increment function
https://github.com/go-rel/mysql/blob/main/mysql.go#L52

@hxgdzyuyi
Copy link
Author

IncrementFunc only invoke in insertAll ?

@Fs02
Copy link
Member

Fs02 commented Jul 7, 2022

yes

@hxgdzyuyi
Copy link
Author

Can I do that?

			dsn := fmt.Sprintf(
				"%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
				ko.String("DB_USERNAME"),
				ko.String("DB_PASSWORD"),
				ko.String("DB_HOST"),
				ko.String("DB_PORT"),
				ko.String("DB_DATABASE"),
			)

			adapter, err := mysql.Open(dsn)

			if err != nil {
				return nil, err
			}

			sp, ok := adapter.(*sql.SQL)

			if !ok {
				return nil, errors.New("adapter 获取错误")
			}

			(*sp).IncrementFunc = nil

			return adapter, nil

@Fs02
Copy link
Member

Fs02 commented Jul 7, 2022

I guess you can, just try it
if you do that, auto_increment_increment will be set to 1

@hxgdzyuyi
Copy link
Author

yes~

@hxgdzyuyi
Copy link
Author

thanks

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