Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Georg Semmler
    @weiznich
    @dennismarwood_gitlab The foreign_key annotations in your BlogTags struct are not correct.
    Dennis Marwood
    @dennismarwood_gitlab
    @weiznich Yes, thank you! I spent too long trying to chase that down. When I read the docs, I was thinking it was talking about the name of the PK in the parent.
    I am curious how it expected the name to be "help: message: No field with column name blog_entry_id" though? Is it looking at the name of the struct and parsing it into snake case?
    Georg Semmler
    @weiznich
    The documentation has details about this behavior:
    Sudhir Dhumal
    @sudhirdhumal289_gitlab

    I recently upgraded the clippy and rust
    and now I'm getting clippy error for Insertable

    error: this lifetime isn't used in the impl
      --> diesel_schema/src/planner/attribute.rs:33:21
       |
    33 | #[derive(Serialize, Insertable)]

    any quick suggestion - coming back to rust after 3-4 months - so may be I'm missing something basic thing

    code in attribute.rs:
    #[derive(Serialize, Insertable)]
    #[table_name = "attributes"]
    pub struct NewAttribute {
        pub display_text: String,
        pub description: Option<String>,
        pub data_type: AttributeValueDataType,
    }
    Georg Semmler
    @weiznich
    @sudhirdhumal289_gitlab Thats diesel-rs/diesel#3182
    The corresponding fix is included in the recent 2.0.0-rc.q release.
    Sudhir Dhumal
    @sudhirdhumal289_gitlab
    Ok
    Sudhir Dhumal
    @sudhirdhumal289_gitlab
    tried using 2* version seems like I have to look at the from-sql, to-sql and schema file change
    so many errors to resolve
    Georg Semmler
    @weiznich
    As the version number indicates that release contains breaking changes.
    Checkout the migration guide draft for possible solutions. https://github.com/diesel-rs/diesel/blob/master/guide_drafts/migration_guide.md
    Gus Power
    @guspower_twitter

    Hi - I'm trying to deserialize some numeric fields from postgresql to always have a fixed precision of 7
    I'm using bigdecimal, have created a custom type RoundedBigDecimaland am using #[diesel(deserialize_as = "RoundedBigDecimal")]
    The target struct derives Queryable (#[derive(Identifiable, Queryable)])

    However I get a rustc error: ^^^^ the trait Queryable<diesel::sql_types::Numeric, Pg> is not implemented for RoundedBigDecimal

    a) is this a sensible approach and b) how can I satisfy this trait?

    implementation:
    struct RoundedBigDecimal(BigDecimal);
    
    impl Into<BigDecimal> for RoundedBigDecimal {
        fn into(self) -> BigDecimal {
            self.0
        }
    }
    
    impl<DB, ST> FromSql<ST, DB> for RoundedBigDecimal
    where
        DB: Backend,
        BigDecimal: FromSql<ST, DB>,
    {
        fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
            BigDecimal::from_sql(bytes).map(|s| RoundedBigDecimal(s.with_prec(7)))
        }
    }
    Gus Power
    @guspower_twitter

    aha I added some derived traits and associated RoundedBigDecimal with the Numeric sql type:

    #[derive(Debug, Clone, AsExpression, FromSqlRow)]
    #[sql_type = "Numeric"]
    pub struct RoundedBigDecimal(BigDecimal);

    https://stackoverflow.com/questions/49092437/how-do-i-implement-queryable-and-insertable-for-custom-field-types-in-diesel <- very useful post +1

    Haris
    @Wulf
    hey everyone! just wanted to share that I added support for the diesel v2 release candidate in create-rust-app! Check it out here: https://github.com/Wulf/create-rust-app
    LNow
    @LNow_gitlab
    Hi!
    Do you have any examples how to use Diesel with more than one schema?
    Georg Semmler
    @weiznich
    What exactly are you looking for?
    LNow
    @LNow_gitlab
    I'd like to use 2 schemas in 2 separate applications.
    1st app will have read-write access to "messages" schema
    2nd app will have read access to "messages" and read-write to "data" schema, and in this application I want to be able to execute queries that join tables from both schemas.
    I know how to create app that uses only one schema. But have no idea how to use 2.
    Georg Semmler
    @weiznich
    Well it's not really different for more than one schema. From a pure diesel point of view you just generate two different schema.rs files and use them together. You might want to add some additional joinable! and allow_tables_to_appear_in_same_query! calls to allow combining tables form different schemas, but otherwise it will just work as for a single schema.
    The other thing you probably need to be aware of: diesel_cli assumes that there is a single schema. So you either need to use two different diesel.toml files or be careful which flags to pass when so that the cli tools always use the correct schema. (That written: That's a topic where I would be open for improvements if someone wants to work on that)
    LNow
    @LNow_gitlab
    OK, thank you @weiznich . I'll try that
    Raphael C
    @GreeFine
    Hello !
    I am trying to work with bitwise operation inside my queries
    I looked at the documentation to find if it could be done but didn't find anything, so I used the sql function to do it which work perfectly !
                    user_dsl::users.filter(
                        sql::<Bool>("account_status & ")
                            .bind::<BigInt, _>(AccountStatus::DailyRewardAck as i64)
                            .sql(" = ")
                            .bind::<BigInt, _>(AccountStatus::DailyRewardAck as i64),
                    ),
    Georg Semmler
    @weiznich
    You might want to have a look at diesel_infix_operator! which allows you to extend the dsl to support additional operators: https://docs.diesel.rs/1.4.x/diesel/macro.diesel_infix_operator.html#example-usage
    Raphael C
    @GreeFine
    This is already great but I would like to try to implement the operator directly and avoid using the raw sql
    I tried with diesel_infix_operator but didn't manage to make it work because it take 3 2 arguments (kindof its the same argument twice)
    Georg Semmler
    @weiznich
    Well as far as I see that is using two operators that are chained. So you would define a custom bit_and for the account_status & $1 part and then use the existing .eq(…) operator for the second part.
    Raphael C
    @GreeFine
    okay I will try that ! thanks
    Georg Semmler
    @weiznich
    Otherwise it's always possible to implement such things manually, but that requires more work than using an existing macro
    Raphael C
    @GreeFine
    And could this be a contribution ? implementing default or/and operators for Intergers
    Georg Semmler
    @weiznich
    Generally speaking yes, as long as there are tests + documentation + it's only implemented for cases where it works (backends, …)
    Raphael C
    @GreeFine
    :thumbsup:
    myz-dev
    @myz-dev

    Hi,

    I am trying to retrieve the ID of an inserted item with SQLite3.
    I see that SQLite 3.5 now supports the returning clause and apparently diesel 2.0.0-rc.0 also has implemented the change ( the changelog says: "Added support for RETURNING expressions for Sqlite via the returning_clauses_for_sqlite_3_35 feature").
    Is there a possibility to get the ID of an inserted row with:

    diesel = {version = "1.4.8", features = ["sqlite"]}
    SQLlite3 3.31.1

    Or do I need to upgrade my environment accordingly?
    So far I am trying to follow this Stack Overflow answer that suggests to use the SQLite last_insert_rowid() function. Unfortunately I have no idea when exactly to call this function. What I have tried so far is to define that function:

    diesel::no_arg_sql_function!(
            last_insert_rowid,
            diesel::sql_types::Integer,
            "last_insert_rowid()"
        );

    When I have an insert statement like this:

    match diesel::insert_into(my_table)
            .values(&my_insertable_struct)
            .execute(&my_db_connection)
        {
            Ok(written_count) => (),
            Err(e) => (),
        }

    Where and how would I call that function? The return type in the Ok() path is now the count of the inserted rows.
    Would this turn into the last inserted row ID if I called the last_insert_rowid() function?
    Sorry for this stupid question, I am pretty clueless here.

    Georg Semmler
    @weiznich
    @myz-dev For 1.4.x that's the correct approach. You need to have a separate select statement, which calls last_inserted_rowid. So something like diesel::select(last_inserted_rowid).get_result::<i64>(conn)?
    Normally you would wrap both the insert and the select into a database transaction to ensure that they agree on the state of the database.
    As alternative solution you can also order your table by your autoincrementing id and select the entry with the largest id.
    myz-dev
    @myz-dev
    Thank you very much for your answer. I assumed that somehow I could put the last_inserted_rowid into the insert statement so that I would not have to query a second time and risk a race condition.
    I will now read up about transactions, thank you very much!
    Georg Semmler
    @weiznich
    There is unfortunately no other way than having 2 queries. (At least not without returning, which as you noticed is only available on 2.0)
    (BTW: It's likely fine to just use the latest 2.0.0 RC version for anything that's not a really large deployed application)
    myz-dev
    @myz-dev
    I am just reading the migration guide :-)
    Daniel Brooks
    @db48x
    I’m having some difficulty figuring out this error:
    139  |       .first(&connection)
         |        ^^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Text>, Sqlite>` is not implemented for `*const str`
    Georg Semmler
    @weiznich
    That basically says you've tried to load a Nullable<Text> field in a String, which is not nullable.
    Daniel Brooks
    @db48x
    hmm
    I’m actually trying to store it in an Option<String>
    Georg Semmler
    @weiznich
    The error message indicates something different. It's hard to answer that with more details without knowing the relevant code. You might need to check that all fields match in type and order with your query.
    Daniel Brooks
    @db48x
    oh
    I’m calling .first(…).optional(), so I guess I’d better store it in an Option<Option<String>> :P
    Georg Semmler
    @weiznich
    Yes, that's required then. The outer Option<_> signals whether one or no entry was found, while the inner one signal whether the entry was null or not.
    Niels Cautaerts
    @din14970

    I'm trying to select rows by filtering on a boolean column in a table in sqlite and facing a strange issue. My table schema is basically

    table! {
        table_name (id) {
            id -> Integer,
            name -> Text,
            bool_column -> Bool,
        }
    }

    All of my rows have the bool column to false, which I have verified. However, the following returns me nothing

    table_name.filter(bool_column.eq(false)).load::<Table>(connection)

    whereas I do get all the rows if I get rid of the filter statement. Is this expected behavior? What is the correct way to do this?

    Niels Cautaerts
    @din14970

    ok I figured out it has to do with the way I created the table. In SQL I had

    bool_column BOOLEAN NOT NULL DEFAULT 'f'

    as in the main docs example. It should have been DEFAULT 0 as per the example for sqlite in the repo