Browse Source

User.save don't use replace_into

pull/6262/head
Timshel 5 days ago
parent
commit
5cb8fee0ad
  1. 19
      src/db/models/user.rs

19
src/db/models/user.rs

@ -284,21 +284,22 @@ impl User {
db_run! {conn: db_run! {conn:
sqlite, mysql { sqlite, mysql {
match diesel::replace_into(users::table) let value = UserDb::to_db(self);
.values(UserDb::to_db(self)) // Don't use replace_into() since it wants to delete the record first.
match diesel::update(users::table)
.filter(users::uuid.eq(&self.uuid))
.set(&value)
.execute(conn) .execute(conn)
{ {
Ok(_) => Ok(()), Ok(1) => Ok(()),
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first. Ok(_) => {
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => { diesel::insert_into(users::table)
diesel::update(users::table) .values(value)
.filter(users::uuid.eq(&self.uuid))
.set(UserDb::to_db(self))
.execute(conn) .execute(conn)
.map_res("Error saving user") .map_res("Error saving user")
} }
Err(e) => Err(e.into()), Err(e) => Err(e.into()),
}.map_res("Error saving user") }.map_res("Error updating user")
} }
postgresql { postgresql {
let value = UserDb::to_db(self); let value = UserDb::to_db(self);

Loading…
Cancel
Save