Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Guillaume Balaine
    @Igosuki

    I have a question regarding diesel, if it is at all possible to implement with the current traits. I am using sqlite :

    #[derive(serde::Deserialize)]
    struct B {
      some_field: String
    }
    #[derive(diesel::Queryable)]
    struct A {
      id: String,
      bs: Vec<B>
    }

    I'd like to serialize A.bs as json (it's a text column) when serializing with diesel

    I tried implementing ToSql and FromSql, but I can't implement it for Vec obviously, is there a solution here ?
    I did
    impl FromSql<Text, diesel::sqlite::Sqlite> for B {
        fn from_sql(bytes: Option<&<diesel::sqlite::Sqlite as Backend>::RawValue>) -> diesel::deserialize::Result<Self> {
            let t = <String as FromSql<Text, diesel::sqlite::Sqlite>>::from_sql(bytes)?;
            Ok(serde_json::from_str(&t)?)
        }
    }
    
    impl ToSql<Text, diesel::sqlite::Sqlite> for B {
        fn to_sql<W: Write>(&self, out: &mut Output<W, diesel::sqlite::Sqlite>) -> diesel::serialize::Result {
            let s = serde_json::to_string(&self)?;
            <String as ToSql<Text, diesel::sqlite::Sqlite>>::to_sql(&s, out)
        }
    }
    Georg Semmler
    @weiznich
    You want to use diesel::associations + #[serde(flatten)] for that.
    Guillaume Balaine
    @Igosuki
    Yeah but I always manipulate this array in its entirety I don't want to put it in a separate table
    It's just an array of tuple of floats
    Georg Semmler
    @weiznich
    It's as sting in the same table?
    Guillaume Balaine
    @Igosuki
    create table a (id varchar primary key, bs text not null)
    Georg Semmler
    @weiznich
    Then that's the #[deserialize_as ="…"] feature for Queryable
    See the documentation there.
    Guillaume Balaine
    @Igosuki
    I have a few fields like these that are simple structs but don't belong a separate table, I don't want to use pg right now for cost reasons
    Guillaume Balaine
    @Igosuki
    Thanks, that works if I can implement Queryable, for B, but I can't implement Queryable for Vec<B>
    Guess I have to wrap that in another struct such as struct Bs(Vec<B>), wish there was another way tho
    Georg Semmler
    @weiznich
    #[diesel(deserialize_as ="…")] should allow you to have an Vec<B> in your struct, but the impls must be done for a new type wrapper due to the orphan rule.
    Guillaume Balaine
    @Igosuki
    yeah it's what I did, gotta put this in a macro though because it's too repetitive
    pfly9000
    @pfly9000
    I have a table that I create through a migration with the following sql:
    CREATE TABLE portfolio_headers
    (
    db_portfolio_id SERIAL,
    cusip VARCHAR,
    isin VARCHAR,
    nsccportid VARCHAR,
    description VARCHAR,
    trade_date DATE,
    etf_agent VARCHAR,
    component_count INTEGER,
    cash_amt_per_cu DOUBLE PRECISION,
    net_asset_value_per_cu DOUBLE PRECISION,
    cash_amt_per_etf DOUBLE PRECISION,
    net_asset_val_per_etf DOUBLE PRECISION,
    total_cash_amt_per_cu DOUBLE PRECISION,
    total_shares_out_per_etf BIGINT,
    dividend_amt_per_etf DOUBLE PRECISION,
    est_val_cash_in_lieu_per_cu DOUBLE PRECISION,
    is_cash_only BOOLEAN,
    expense_ratio BIGINT,
    tot_net_asset_value DOUBLE PRECISION,
    etf_shares_per_create_redeem BIGINT,
    etf_symbol VARCHAR NOT NULL,
    cns_eligibility_ind VARCHAR,
    can_etf_create_redeem BOOLEAN,
    prior_day_portfolio_used BOOLEAN,
    basket_type INTEGER,
    is_domestic BOOLEAN,
    is_new_portfolio BOOLEAN,
    etf_basket_unit_size BIGINT,
    projected_cash_for_pricing_basket DOUBLE PRECISION,
    baskets_outstanding BIGINT,
    use_standard_basket_for_pricing BOOLEAN,
    is_standard_creation_order_usage BOOLEAN,
    is_standard_redemption_order_usage BOOLEAN,
    PRIMARY KEY (db_portfolio_id, trade_date,etf_symbol, basket_type)
    );
    Sorry about how large this is
    diesel creates the following schema:
    table! {
    portfolio_headers (db_portfolio_id, trade_date, etf_symbol, basket_type) {
    db_portfolio_id -> Int4,
    cusip -> Nullable<Varchar>,
    isin -> Nullable<Varchar>,
    nsccportid -> Nullable<Varchar>,
    description -> Nullable<Varchar>,
    trade_date -> Date,
    etf_agent -> Nullable<Varchar>,
    component_count -> Nullable<Int4>,
    cash_amt_per_cu -> Nullable<Float8>,
    net_asset_value_per_cu -> Nullable<Float8>,
    cash_amt_per_etf -> Nullable<Float8>,
    net_asset_val_per_etf -> Nullable<Float8>,
    total_cash_amt_per_cu -> Nullable<Float8>,
    total_shares_out_per_etf -> Nullable<Int8>,
    dividend_amt_per_etf -> Nullable<Float8>,
    est_val_cash_in_lieu_per_cu -> Nullable<Float8>,
    is_cash_only -> Nullable<Bool>,
    expense_ratio -> Nullable<Int8>,
    tot_net_asset_value -> Nullable<Float8>,
    etf_shares_per_create_redeem -> Nullable<Int8>,
    etf_symbol -> Varchar,
    cns_eligibility_ind -> Nullable<Varchar>,
    can_etf_create_redeem -> Nullable<Bool>,
    prior_day_portfolio_used -> Nullable<Bool>,
    basket_type -> Int4,
    is_domestic -> Nullable<Bool>,
    is_new_portfolio -> Nullable<Bool>,
    etf_basket_unit_size -> Nullable<Int8>,
    projected_cash_for_pricing_basket -> Nullable<Float8>,
    baskets_outstanding -> Nullable<Int8>,
    use_standard_basket_for_pricing -> Nullable<Bool>,
    is_standard_creation_order_usage -> Nullable<Bool>,
    is_standard_redemption_order_usage -> Nullable<Bool>,
    }
    }
    But when I try and compile I get the following error:
    90 | / table! {
    91 | | portfolio_headers (db_portfolio_id, trade_date, etf_symbol, basket_type) {
    92 | | db_portfolioid -> Int4,
    93 | | cusip -> Nullable<Varchar>,
    ... |
    125 | | }
    126 | | }
    | |
    ^ the trait diesel::Expression is not implemented for (portfolio_headers::columns::db_portfolio_id, portfolio_headers::columns::cusip, portfolio_headers::columns::isin, portfolio_headers::columns::nsccportid, portfolio_headers::columns::description, portfolio_headers::columns::trade_date, portfolio_headers::columns::etf_agent, portfolio_headers::columns::component_count, portfolio_headers::columns::cash_amt_per_cu, portfolio_headers::columns::net_asset_value_per_cu, portfolio_headers::columns::cash_amt_per_etf, portfolio_headers::columns::net_asset_val_per_etf, portfolio_headers::columns::total_cash_amt_per_cu, portfolio_headers::columns::total_shares_out_per_etf, portfolio_headers::columns::dividend_amt_per_etf, portfolio_headers::columns::est_val_cash_in_lieu_per_cu, portfolio_headers::columns::is_cash_only, portfolio_headers::columns::expense_ratio, portfolio_headers::columns::tot_net_asset_value, portfolio_headers::columns::etf_shares_per_create_redeem, portfolio_headers::columns::etf_symbol, portfolio_headers::columns::cns_eligibility_ind, portfolio_headers::columns::can_etf_create_redeem, portfolio_headers::columns::prior_day_portfolio_used, portfolio_headers::columns::basket_type, portfolio_headers::columns::is_domestic, portfolio_headers::columns::is_new_portfolio, portfolio_headers::columns::etf_basket_unit_size, portfolio_headers::columns::projected_cash_for_pricing_basket, portfolio_headers::columns::baskets_outstanding, portfolio_headers::columns::use_standard_basket_for_pricing, portfolio_headers::columns::is_standard_creation_order_usage, portfolio_headers::columns::is_standard_redemption_order_usage)
    |
    I also get NonAggregate not satisfied and SelectableExpression<portfolio_headers, table> not satisfied.
    I am not quite sure what I am doing wrong here.
    pfly9000
    @pfly9000
    diesel 1.4.4 with postgres and chono
    chrono
    I get the same this with version 1.4.7
    pfly9000
    @pfly9000
    One more piece that may help:
    247 | type Query: Query<SqlType = Self::SqlType>;
    | ----------------------- required by this bound in diesel::query_builder::AsQuery::Query
    |
    = note: this error originates in the macro __diesel_table_impl (in Nightly builds, run with -Z macro-backtrace for more info)
    pfly9000
    @pfly9000
    OK I "solved" this by moving these columns into a new table
    is_domestic BOOLEAN,
    is_new_portfolio BOOLEAN,
    etf_basket_unit_size BIGINT,
    projected_cash_for_pricing_basket DOUBLE PRECISION,
    baskets_outstanding BIGINT,
    use_standard_basket_for_pricing BOOLEAN,
    is_standard_creation_order_usage BOOLEAN,
    is_standard_redemption_order_usage BOOLEAN,
    u2m4c6
    @u2m4c6

    Hello everyone, I am new to Gitter (and Rust), so please let me know if I should use another room for general questions. I am trying to hook Diesel up with an existing Sqlite database and am getting the following error

    thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: DatabaseError(__Unknown, "no such table: col")', src/main.rs:30:34

    I used print schema with the CLI tool to generate the schema.rs file and then manually made a struct for the "col" table in a models.rs file

    The existing database does not declare the table primary keys as "NOT NULL". None of the rows actually have null primary keys. I did not design the database...not sure if the lack of NOT NULL is what is breaking Diesel though
    chroma
    @chroma:mozilla.org
    [m]
    YOu have not provided a minimal reproducible example
    u2m4c6
    @u2m4c6
    I am an idiot...my environment variable was wrong for the db connection string
    so if anyone is searching for this error: there is no such table because you might be pointing to a database that does not exist. triple check your connection string
    Ludwig Von Mises
    @mises_gitlab

    Hi, I am getting an issue when trying to use Insertable with a struct containing NaiveDate.

    error[E0277]: the trait bound `NaiveDate: diesel::Expression` is not satisfied
      --> src/models.rs:10:52
       |
    10 | #[derive(Serialize, Deserialize, Debug, Queryable, Insertable)]
       |                                                    ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `NaiveDate`
       |
       = note: required because of the requirements on the impl of `AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Text>>` for `NaiveDate`
       = note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

    Any thoughts on how to resolve this?

    Ludwig Von Mises
    @mises_gitlab
    By the way, I have included diesel::{Insertable, Queryable}, and everything works fine if I remove the Insertable trait derivation.
    chroma
    @chroma:mozilla.org
    [m]
    @mises_gitlab: great username
    Ludwig Von Mises
    @mises_gitlab
    Why thank you
    chroma
    @chroma:mozilla.org
    [m]
    Have you specified the "chrono" feature in Cargo.toml?
    Ludwig Von Mises
    @mises_gitlab
    Yes, I've got diesel = { version = "1.4.7", features = ["mysql", "extras"] }; extras should include chrono. I also tried replacing extras with chrono; no difference.
    I also have chrono = { version = "0.4", features = ["serde"] }
    chroma
    @chroma:mozilla.org
    [m]
    Is it possible for you to post a minimal reproducible example? https://stackoverflow.com/help/minimal-reproducible-example
    Ludwig Von Mises
    @mises_gitlab
    Ahh, it looks like my issue was having the wrong sql type in my schema.rs. Go figure.
    Georg Semmler
    @weiznich
    @pfly9000 You want to enable the 64-columns feature to enable support for tables with more than 32 columns.
    Pavan Kumar Sunkara
    @pksunkara
    @weiznich Hey, what do you think about removing the keyword changeset_options and just use treat_none_as_null directly in the new syntax?
    Similar to how treat_none_as_default_value is used currently
    Georg Semmler
    @weiznich
    Sound fine :+1: