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

dialect/sql/sqlgraph: fix CreateBulk when Fields in BatchCreateSpec is empty #3945

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

t-coron
Copy link

@t-coron t-coron commented Feb 11, 2024

Fixed #3944 Issue in CreateBulk()

CraeteBulk() always generates wrong query when Fields in batchCreateSpec.Nodes is empty

query=INSERT INTO `groups` VALUES () args=[]

This query is generated by:

insert := c.builder.Insert(c.Nodes[0].Table).Schema(c.Nodes[0].Schema).Default().Columns(sorted...)

If defaults is true and len(sorted) (i.e. len(columns)) is zero, insert builder exec writeDefault(),

ent/dialect/sql/builder.go

Lines 963 to 965 in d4560fa

if i.defaults && len(i.columns) == 0 {
i.writeDefault(&b)
} else {

ent/dialect/sql/builder.go

Lines 982 to 989 in d4560fa

func (i *InsertBuilder) writeDefault(b *Builder) {
switch i.Dialect() {
case dialect.MySQL:
b.WriteString("VALUES ()")
case dialect.SQLite, dialect.Postgres:
b.WriteString("DEFAULT VALUES")
}
}

With this PR, we try to avoid execution of writeDefault.
If we want to insert 3 records through CreateBulk(), the query is:

query=INSERT INTO `groups` (`id`) VALUES (NULL), (NULL), (NULL) args=[]

@t-coron
Copy link
Author

t-coron commented Feb 12, 2024

Umm, this might be better.

// if node.ID != nil && (node.ID.Value != nil || len(node.Fields) == 0) {
if node.ID != nil {

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.

CreateBulk fails when entity has only ID field
1 participant