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

postgres: bulk insert API with UNNEST and ARRAYs #1922

Open
gitmalong opened this issue Oct 18, 2023 · 3 comments
Open

postgres: bulk insert API with UNNEST and ARRAYs #1922

gitmalong opened this issue Oct 18, 2023 · 3 comments

Comments

@gitmalong
Copy link
Sponsor

See my comment here #710 (comment)

@tyt2y3 tyt2y3 changed the title postgres: insert_many should use UNNEST and ARRAYs postgres: bulk insert API with UNNEST and ARRAYs Oct 20, 2023
@tyt2y3
Copy link
Member

tyt2y3 commented Oct 20, 2023

I imagine we can have a bulk insert API that bypasses ActiveModel. Since UNNEST ARRAY is column-oriented.
(It'd also be much easier than try to build on top of the existing Inserter)

Something like:

MyEntity::bulk_insert([
    (my_entity::Column::Id, &[1,2,3,4]),
    (my_entity::Column::Name, &["a","b","c","d"]),
])

That'd build:

INSERT INTO my_entity (id, name) VALUES UNNEST ARRAY($1, $2);
# where $1 is i32[1,2,3,4], $2 is text["a","b","c","d"]

It's still in brainstorming phase so suggestions are welcome.

@gitmalong
Copy link
Sponsor Author

gitmalong commented Oct 20, 2023

From end users perspective I would still like to pass over my whole Vec<MyStruct> and let SeaORM take care of the "field to vec split" instead of setting each column on my own because it is easy to forget something etc. But I don't know about the internals and if we can first start with your suggested bulk_insert and then build something more ergonomic on top of it, it sounds like a good starting point.

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 20, 2023

Yes, that sounds great. We can may be add a fn into_bulk_insert<E: EntityTrait>(items: Vec<E::ActiveModel>) -> BulkInsert<E>

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

No branches or pull requests

2 participants