Skip to content

Insert many#2628

Merged
tyt2y3 merged 6 commits intomasterfrom
insert-2
Jun 18, 2025
Merged

Insert many#2628
tyt2y3 merged 6 commits intomasterfrom
insert-2

Conversation

@tyt2y3
Copy link
Copy Markdown
Member

@tyt2y3 tyt2y3 commented Jun 15, 2025

end goal is to add a new struct for InsertMany, diverging from current Insert

@tyt2y3 tyt2y3 marked this pull request as ready for review June 16, 2025 17:30
@tyt2y3
Copy link
Copy Markdown
Member Author

tyt2y3 commented Jun 16, 2025

Summary of changes:

  1. removed some apis so panic is impossible
  2. new helper struct InsertMany
  3. last_insert_id is now Option<Value>
  4. on empty returns None or vec![] on exec operations
  5. on conflict clause, if added, returns DbErr::RecordNotInserted, same as before
  6. TryInsert API is unchanged

@Huliiiiii @zevweiss @langyo

@tyt2y3 tyt2y3 requested review from Expurple and billy1624 June 16, 2025 17:48
Comment on lines +24 to +32
let res = Entity::insert_many::<ActiveModel, _>([]).exec(db).await;

assert_eq!(res?.last_insert_id, None);

let res = Entity::insert_many([ActiveModel { id: Set(1) }, ActiveModel { id: Set(2) }])
.exec(db)
.await;

assert_eq!(res?.last_insert_id, Some(2));
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

best to look at this file for illustration

Comment on lines +108 to +114
assert!(
Entity::insert_many::<ActiveModel, _>([])
.exec_with_returning(db)
.await
.unwrap()
.is_empty()
);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example

A: ActiveModelTrait,
{
/// The id performed when AUTOINCREMENT was performed on the PrimaryKey
pub last_insert_id: Option<<PrimaryKey<A> as PrimaryKeyTrait>::ValueType>,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type makes a lot of sense. The PR is worth it, in my opinion

Comment on lines +57 to +60
/// Empty: `Ok(None)`. Inserted: `Ok(Some(last_insert_id))`. Conflicted: `Err(DbErr::RecordNotInserted)`.
pub fn last_insert_id(
self,
) -> Result<Option<<PrimaryKey<A> as PrimaryKeyTrait>::ValueType>, DbErr> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good signature, nice

Comment on lines +301 to +303
/// # Panics
///
/// Panics if the database backend does not support `INSERT RETURNING`.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll replace the panics with BackendNotSupported in a later PR

@tyt2y3 tyt2y3 linked an issue Jun 16, 2025 that may be closed by this pull request
@tyt2y3 tyt2y3 merged commit 13c6533 into master Jun 18, 2025
36 checks passed
@tyt2y3 tyt2y3 deleted the insert-2 branch June 18, 2025 16:44
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.

on_empty_do_nothing() should be enabled by default

2 participants