-
I have a struct: #[derive(Debug, Deserialize, Serialize, Insertable, AsChangeset)]
#[diesel(table_name = crate::schema::publication_poll_cache)]
pub struct PublicationPollCache {
/// Option is ok for PK on insertable, but not queryable, structs
pub id: Option<i32>,
pub user_id: i32,
pub source: PollSource,
pub last_checked: chrono::naive::NaiveDateTime,
pub last_results: serde_json::Value,
} and I attempt to update the fields Relevant code (note that the variable let last_results = serde_json::to_value(csl).expect("Must convert");
let cache_id: Option<i32> = ppc_dsl::publication_poll_cache
.select(ppc_dsl::id)
.filter(ppc_dsl::user_id.eq(user.id))
.filter(ppc_dsl::source.eq(source.to_string()))
.get_result::<i32>(connection)
.optional()?;
let cache_row = PublicationPollCache {
id: cache_id,
user_id: user.id,
source,
last_checked: chrono::Utc::now().naive_local(),
// TODO revert below
last_results: serde_json::Value::Null
};
tracing::trace!("cache_row to insert: {:?}", cache_row);
// Upsert via ON CONFLICT (id) DO UPDATE SET id = EXCLUDED.id
let _x = diesel::insert_into(ppc_dsl::publication_poll_cache)
.values(&cache_row)
.on_conflict(ppc_dsl::id)
.do_update()
.set(ppc_dsl::id.eq(excluded(ppc_dsl::id)))
.execute(connection)?;
tracing::debug!(user = user.email, "poll cache updated"); Any ideas where I am going wrong? Is it the fact that I do not have Diesel version: 2.2.1 (but I didn't see anything in the subsequent changelogs that addresses this) Thanks to all in advance for pointers |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Asked and Answered: Needing to specify ALL fields after a conflict is a postgres limitation; diesel supports using an AsChangeset struct in the
|
Beta Was this translation helpful? Give feedback.
Asked and Answered:
Needing to specify ALL fields after a conflict is a postgres limitation; diesel supports using an AsChangeset struct in the
.set
ala:.do_update().set(&cache_row)...