Is there a way of excluding specific fields from select queries? #3379
-
i.e. without creating a new struct, do not select a specific (Optional) column such that is it is always |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 11 replies
-
The best way to achieve that would likely be to not mention it in the select expression of your query. If you need to add an extra empty field on the fly without making an extra allocation, you may use |
Beta Was this translation helpful? Give feedback.
-
Is there a way to select a raw value in the column's place? If I do this: #[derive(AsChangeset, Default, Insertable, Queryable, Serialize)]
#[diesel(table_name = org_transaction)]
pub struct OrgTransaction {
pub id: String,
pub org_id: String,
pub amount: i64,
pub currency_code: String,
pub merchant_name: String,
pub initiated_at: Option<chrono::NaiveDate>,
pub raw_transaction: Option<String>,
pub created_at: chrono::NaiveDateTime,
}
org_transaction::table
.filter(org_transaction::org_id.eq(org_id))
.select((
org_transaction::id,
org_transaction::org_id,
org_transaction::amount,
org_transaction::currency_code,
org_transaction::merchant_name,
org_transaction::initiated_at,
None as Option<String>,
org_transaction::created_at,
))
.order(org_transaction::initiated_at.desc())
.load::<OrgTransaction>(connection)
.expect("Error reading org_transactions") I get the error: org_transaction::table
.filter(org_transaction::org_id.eq(org_id))
.select((
org_transaction::id,
org_transaction::org_id,
org_transaction::amount,
org_transaction::currency_code,
org_transaction::merchant_name,
org_transaction::initiated_at,
org_transaction::created_at,
))
.order(org_transaction::initiated_at.desc())
.load::<OrgTransaction>(connection)
.expect("Error reading org_transactions") I get an error on the LoadQuery implying I cannot satisfy the struct fields |
Beta Was this translation helpful? Give feedback.
-
In addition the the already provided answer: You can always implement |
Beta Was this translation helpful? Give feedback.
-
Idk if you can close discussions, but both methods work for me now (implementing Queryable with a subset of the sql columns, and selecting a raw value and converting it into sql) |
Beta Was this translation helpful? Give feedback.
-
Two ways of going about this:
use diesel::{self, sql_types};
use diesel::prelude::*;
use crate::schema::item;
#[derive(AsChangeset, Debug, Insertable, Queryable)]
#[diesel(table_name = item)]
pub struct Item {
pub id: String,
pub name: String,
pub raw_json: Option<String>, // this is the field we want to exclude
pub created_at: chrono::NaiveDateTime,
}
pub fn read_items_by_ids(
connection: &mut MysqlConnection,
ids: Vec<&String>,
) -> Vec<Item> {
item::table
.select(
item::id,
item::name,
None::<String>.into_sql::<sql_types::Nullable<sql_types::Text>>(),
item::created_at,)
.filter(item::id.eq_any(ids))
.load::<Item>(connection)
.expect("Error reading items")
}
use diesel;
use diesel::prelude::*;
use diesel::sql_types::*;
use crate::schema::item;
#[derive(AsChangeset, Debug, Insertable)]
#[diesel(table_name = item)]
pub struct Item {
pub id: String,
pub name: String,
pub raw_json: Option<String>, // this is the field we want to exclude
pub created_at: chrono::NaiveDateTime,
}
type SqlWithoutRawJson = (
Char,
Varchar,
Timestamp,
);
impl Queryable<SqlWithoutRawJson, diesel::mysql::Mysql> for Item {
type Row = (
String,
String,
chrono::NaiveDateTime,
);
fn build(row: Self::Row) -> diesel::deserialize::Result<Self> {
let (
id,
name,
created_at,
) = row;
Ok(Item {
id,
name,
created_at,
raw_json: None,
})
}
}
pub fn read_items_by_ids(
connection: &mut MysqlConnection,
ids: Vec<&String>,
) -> Vec<Item> {
item::table
.select(
item::id,
item::name,
item::created_at,
)
.filter(item::id.eq_any(ids))
.load::<Item>(connection)
.expect("Error reading items")
} Queryable has the added advantage of being a trait you can implement multiple times, for different sets of required fields to be selected Thanks @weiznich and @Ten0 for the help; I wanted to summarize your answers/what worked for me here |
Beta Was this translation helpful? Give feedback.
Two ways of going about this: