Use insert_into when possible (#6437)

Co-authored-by: Timshel <timshel@users.noreply.github.com>
This commit is contained in:
TimshelandTimshel authored and GitHub committed 2026-09-09 16:43:47 +02:00
1 parent e992cbb4f5
commit 25dfedafd7
14 files changed
+188 -333

No files matched your search

+7 -15
View File
@@ -82,24 +82,16 @@ impl Attachment {
impl Attachment {
pub async fn save(&self, conn: &DbConn) -> EmptyResult {
db_run! { conn:
sqlite, mysql {
match diesel::replace_into(attachments::table)
mysql {
diesel::insert_into(attachments::table)
.values(self)
.on_conflict(diesel::dsl::DuplicatedKeys)
.do_update()
.set(self)
.execute(conn)
{
Ok(_) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first.
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(attachments::table)
.filter(attachments::id.eq(&self.id))
.set(self)
.execute(conn)
.map_res("Error saving attachment")
}
Err(e) => Err(e.into()),
}.map_res("Error saving attachment")
.map_res("Error saving attachment")
}
postgresql {
postgresql, sqlite {
diesel::insert_into(attachments::table)
.values(self)
.on_conflict(attachments::id)