Skip to content

Pass an iterator to CreateInBatches #7430

@brvoisin

Description

@brvoisin

Hi all!

Your Question

I tried to pass an iterator to the method CreateInBatches:

	modelsIterator := func(contacts []domain.Contact) iter.Seq[contactModel] {
		return func(yield func(contactModel) bool) {
			for _, contact := range contacts {
				model := domainToModel(contact)
				if !yield(model) {
					return
				}
			}
		}
	}

	tx := db.CreateInBatches(modelsIterator(contacts), 1000)

But I got this error:

unsupported data type: iter.Seq[contactModel]: Table not set, please set it like: db.Model(&user) or db.Table("users")

Are iterators supported by CreateInBatches?

The document you expected this should be explained

It's not mentioned in Create Batch Insert.

Expected answer

Is there a way to create in batches with an iterator to avoid allocating a big array in memory?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions