A
A
Alexander Vladimirovich2020-03-24 11:20:26
go
Alexander Vladimirovich, 2020-03-24 11:20:26

How to bulk insert in GORM?

Greetings!
There is an array of structures that need to be written to the appropriate database table using GORM.
Now I do it with a loop and gorm DB.Create one at a time.
How to save them all at once? DB.Create does not accept an array.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2020-03-24
@polyanin

You can use the library https://github.com/t-tiger/gorm-bulk-insert
If you don't want to pull unnecessary dependencies into the project, you can wrap the cycle in a transaction, this will give a performance gain close to normal bulk insert.
Approximately like this:

tx.Begin()
for {
   DB.Create
}
tx.Commit()

I advise you to take a closer look at this library https://jmoiron.github.io/sqlx/
I use it in almost all projects.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question