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

PostgreSQL insert many with returning #1021

Merged
merged 1 commit into from
Jan 11, 2023
Merged

Conversation

billy1624
Copy link
Member

PR Info

Breaking Changes

  • PostgreSQL insert many with returning - throw DbErr::RecordNotInserted if none of the records are being inserted

src/error.rs Outdated
@@ -17,6 +17,10 @@ pub enum DbErr {
Json(String),
/// A migration error
Migration(String),
/// None of the records are being inserted into the database,
/// if you insert with upcert expression that means
Copy link
Member

Choose a reason for hiding this comment

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

upsert

@tyt2y3
Copy link
Member

tyt2y3 commented Oct 9, 2022

Thank you. Since #1002 has been merged, I'd like this to nest under the RuntimeError as discussed.

@billy1624 billy1624 force-pushed the pg-insert-many-returning branch from 6d3ba03 to 9d0f3f6 Compare January 5, 2023 08:33
@billy1624 billy1624 force-pushed the pg-insert-many-returning branch from 9d0f3f6 to 6f44907 Compare January 5, 2023 08:34
@billy1624
Copy link
Member Author

Thank you. Since #1002 has been merged, I'd like this to nest under the RuntimeError as discussed.

I think some of the errors that reside at the DbErr root should be a variant of RuntimeError as well, such as UnpackInsertId and UpdateGetPrimaryKey.

So, to be consistent I'd like RecordNotInserted to be consistent with those.

sea-orm/src/error.rs

Lines 5 to 75 in 9d0f3f6

/// An error from unsuccessful database operations
#[derive(Error, Debug)]
pub enum DbErr {
/// This error can happen when the connection pool is fully-utilized
#[error("Failed to acquire connection from pool")]
ConnectionAcquire,
/// Runtime type conversion error
#[error("Error converting `{from}` into `{into}`: {source}")]
TryIntoErr {
/// From type
from: &'static str,
/// Into type
into: &'static str,
/// TryError
source: Box<dyn std::error::Error + Send + Sync>,
},
/// There was a problem with the database connection
#[error("Connection Error: {0}")]
Conn(#[source] RuntimeErr),
/// An operation did not execute successfully
#[error("Execution Error: {0}")]
Exec(#[source] RuntimeErr),
/// An error occurred while performing a query
#[error("Query Error: {0}")]
Query(#[source] RuntimeErr),
/// Type error: the specified type cannot be converted from u64. This is not a runtime error.
#[error("Type '{0}' cannot be converted from u64")]
ConvertFromU64(&'static str),
/// After an insert statement it was impossible to retrieve the last_insert_id
#[error("Failed to unpack last_insert_id")]
UnpackInsertId,
/// When updating, a model should know its primary key to check
/// if the record has been correctly updated, otherwise this error will occur
#[error("Failed to get primary key from model")]
UpdateGetPrimaryKey,
/// The record was not found in the database
#[error("RecordNotFound Error: {0}")]
RecordNotFound(String),
/// Thrown by `TryFrom<ActiveModel>`, which assumes all attributes are set/unchanged
#[error("Attribute {0} is NotSet")]
AttrNotSet(String),
/// A custom error
#[error("Custom Error: {0}")]
Custom(String),
/// Error occurred while parsing value as target type
#[error("Type Error: {0}")]
Type(String),
/// Error occurred while parsing json value as target type
#[error("Json Error: {0}")]
Json(String),
/// A migration error
#[error("Migration Error: {0}")]
Migration(String),
/// None of the records are being inserted into the database,
/// if you insert with upsert expression that means
/// all of them conflict with existing records in the database
#[error("RecordNotInserted Error: {0}")]
RecordNotInserted(String),
}
/// Runtime error
#[derive(Error, Debug)]
pub enum RuntimeErr {
/// SQLx Error
#[cfg(feature = "sqlx-dep")]
#[error("{0}")]
SqlxError(SqlxError),
/// Error generated from within SeaORM
#[error("{0}")]
Internal(String),
}

Comment on lines +145 to +148
let rows = db.query_all(statement).await?;
let res = rows.last().ok_or_else(|| {
DbErr::RecordNotInserted("None of the records are being inserted".to_owned())
})?;
Copy link
Member

Choose a reason for hiding this comment

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

Actually after a second thought I think it all makes sense.
Because there is no SQLx error at all (if there is, we'd like to keep the source)

@billy1624 billy1624 merged commit b5e984c into master Jan 11, 2023
@billy1624 billy1624 deleted the pg-insert-many-returning branch January 11, 2023 06:16
tyt2y3 added a commit that referenced this pull request Jan 19, 2023
denwong47 pushed a commit to denwong47/sea-orm that referenced this pull request Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Inserting active models by insert_many with on_conflict and do_nothing panics if no rows are inserted.
2 participants