Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@onobrod
Copy link
Contributor

@onobrod onobrod commented Aug 10, 2022

Fixed #2000

After #2011 immutable fields are still being ignored in case of bulk upsert. Example of generated code before fix:

func (u *MyEntityUpsertBulk) UpdateNewValues() *MyEntityUpsertBulk {
	u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
	u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
		for _, b := range u.create.builders {
			if _, exists := b.mutation.ID(); exists {
				s.SetIgnore(myentity.FieldID)
				return // <-- if id exists, we will never call SetIgnore for created_at and created_by
			}
			if _, exists := b.mutation.CreatedAt(); exists {
				s.SetIgnore(myentity.FieldCreatedAt)
			}
			if _, exists := b.mutation.CreatedBy(); exists {
				s.SetIgnore(myentity.FieldCreatedBy)
			}
		}
	}))
	return u
}

@a8m
Copy link
Member

a8m commented Aug 12, 2022

Thanks for the contribution, @onobrod 🙏

Changes look good, but I'll add tests to it locally and merge it.

@a8m a8m merged commit 7eb1ccd into ent:master Aug 12, 2022
@a8m
Copy link
Member

a8m commented Aug 12, 2022

Thanks @onobrod 🚀

@a8m
Copy link
Member

a8m commented Aug 12, 2022

Please, you're welcome to join our Discord community.

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.

Upserts can cause Immutable Fields to Mutate

2 participants