Skip to content

Is there a way of excluding specific fields from select queries? #3379

Answered by thomasmost
thomasmost asked this question in Q&A
Discussion options

You must be logged in to vote

Two ways of going about this:

  1. Select a declared value and convert it to SQL
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,)
      

Replies: 5 comments 11 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
10 replies
@Ten0
Comment options

@thomasmost
Comment options

@thomasmost
Comment options

@weiznich
Comment options

@thomasmost
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@weiznich
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by thomasmost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants