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

add CreateInAutoBatches method #6210

Closed
wants to merge 1 commit into from
Closed

add CreateInAutoBatches method #6210

wants to merge 1 commit into from

Conversation

ken2403
Copy link

@ken2403 ken2403 commented Apr 4, 2023

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

API implemented for this issue.

User Case Description

A method to execute Create with appropriate batch size depending on the number of fields of the struct and array size when the parameter limit is restricted.

// big array
values := [10000]{User{Name: "Smith", Age: 33, From: "Canada"}, ...}
// For SQLServer, the maximum number of parameters is 2100
err := db.CreateInAutoBatches(values, 2100).Error

@ken2403 ken2403 changed the title add method add CreateInAutoBatches method Apr 4, 2023
Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to #6142 (comment) ,this feat can be implemented by users in their own utils.
In fact, NumField does not represent the number of parameters, gorm also supports embedded and field permissions

@ken2403
Copy link
Author

ken2403 commented Apr 7, 2023

Thanks for your comment.
I agree that this PR is overengineering, as I have tried many things and it seems that I can implement something equivalent to this by making full use of Gorm's API.

I will continue to support Gorm's simple but powerful API!
Thanks for the kind review and thank you for your time.

By the way, could you please provide a reference to embedded and field permissions?

@a631807682
Copy link
Member

embedded

 type Author struct {
  Name  string
  Email string
}

type Blog struct {
  ID      int
  Author  Author `gorm:"embedded"`
  Upvotes int32
}

https://gorm.io/docs/models.html#Embedded-Struct
https://github.com/go-gorm/gorm/blob/master/schema/field.go#L83
field permissions

	type CustomizeFieldStruct struct {
		gorm.Model
		Name                    string
		FieldAllowCreate        string `gorm:"<-:create"`
		FieldAllowUpdate        string `gorm:"<-:update"`
		FieldAllowSave          string `gorm:"<-"`
		FieldAllowSave2         string `gorm:"<-:create,update"`
		FieldAllowSave3         string `gorm:"->:false;<-:create"`
		FieldReadonly           string `gorm:"->"`
		FieldIgnore             string `gorm:"-"`
	}

https://gorm.io/docs/models.html#Fields-Tags
https://github.com/go-gorm/gorm/blob/master/schema/field.go#L62

@ken2403 ken2403 closed this Apr 7, 2023
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.

None yet

2 participants