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
    What's wrong with that one?
    anabelian
    @anabelian
    er, i have a table with a tsvector column
    but i can't derive Queryable for the associated struct like i would otherwise
    because the library doesn't define (i think?) a FromSql instance for the TsVector type
    i believe, with that library, it is possible to work with tsvector columns as long as you only use them in expressions inside queries
    Georg Semmler
    @weiznich
    As far as I understood full text search you never need to load a TsVector because it's a internal thing.
    anabelian
    @anabelian
    like here
    hmm that's probably right
    Georg Semmler
    @weiznich
    (But I may be wrong, because I've never used that anywhere…)
    anabelian
    @anabelian
    is there a way to get queryable and somehow skip a field?
    Georg Semmler
    @weiznich
    Just pass a .select(your_columns) to your query :wink:
    anabelian
    @anabelian
    or can i write some sort of stub implementation of fromsql/queryable for the tsvector type?
    so then id have to write a helper function that takes the resulting tuple and turns that into a value of a struct type
    ah i guess that'd work but i was hoping for a better way
    Georg Semmler
    @weiznich
    Queryable is named Queryable because it represents a result of a query. It's not necessarily bound to a specific table. A table could have multiple structs implementing Queryable and a struct implementing Queryable could contain fields from multiple tables.
    anabelian
    @anabelian
    wait
    ohh
    so i could do .select(columns).get_results::<ConvenientStruct>()?
    Georg Semmler
    @weiznich
    Yes
    anabelian
    @anabelian
    ah i didn't realise that at all, thank you!
    Georg Semmler
    @weiznich
    No worries, that seems to be a common issue. Maybe I should really write some kind of FAQ mention that (and some other points) sometime …
    anabelian
    @anabelian
    mhm
    also is there some place in the source you could point me to so i could figure out how to write to/fromsql impls for my own enums?
    i did look at this
    Georg Semmler
    @weiznich
    That's another point for the FAQ :see_no_evil:
    anabelian
    @anabelian
    but my schema requires me to back the enum in the database with a string, not an integer
    ooh that looks useful
    Georg Semmler
    @weiznich
    Basically there are two parts:
    1. The sql type (MyType in this case). This is required if you create a custom data type on sql side. If you map to a Integer/String/whatever this is not required.
    2. The rust type (MyEnum in this case). This could be an arbitrary rust type, as long as you could provide meaningful implementations of FromSql/ToSql. If you skipped point 1. because you use a buildin type on db side, just use that sql type in place of MyType there.
    In the second case you should write the FromSql/ToSql implementation in terms of existing implementations. So for the sql type Text just call the FromSql/ToSql implementation of String.
    anabelian
    @anabelian
    ahh i realise why what i was trying didn't work
    impl<DB> FromSql<String, DB> for Rating
    where
        DB: Backend,
        i32: /* hmmm */ FromSql<String, DB>,
    {
        fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
            match String::from_sql(bytes)?.as_str() {
                "s" => Ok(Rating::S),
                // etc
                x => Err(format!("Unrecognized variant {}", x).into()),
            }
        }
    }
    facepalm
    no wonder the compiler simply threw its hands up and said it couldn't resolve the backend
    anabelian
    @anabelian
    it'd really help if trying to derive AsExpression without a #[sql_type = whatever] annotation gave some useful compile-time message
    was stuck until i finally gave in and went and read the AsExpressiondocs
    Georg Semmler
    @weiznich
    What does happen if you miss the attribute?
    anabelian
    @anabelian
    error[E0277]: the trait bound `models::post::Rating: diesel::Expression` is not satisfied
      --> src/models/post.rs:66:24
       |
    66 | #[derive(Debug, Clone, Insertable)]
       |                        ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `models::post::Rating`
    i was about to actually implement Expression by hand haha
    Georg Semmler
    @weiznich
    https://github.com/diesel-rs/diesel/blob/master/diesel_derives/src/as_expression.rs#L117 Someone needs to call Diagnostic::warn(…).emit()/Diagnostic::error(…).emit() there. Mind opening a PR?
    anabelian
    @anabelian
    i can't atm but if it's okay to do it tomorrow sure!
    it's pretty late where i am
    Georg Semmler
    @weiznich
    No worries, take your time :wink:
    anabelian
    @anabelian
    also i've never looked at actual rust macro code ... :scream:
    not before this
    Georg Semmler
    @weiznich
    It's just rust code doing strange things …
    Pankaj Jangid
    @jangid
    In a reasonably sized application different teams working on model and controller part. Is it good idea to hide low level diesel apis under application level abstraction? For example allow only User::create in practice instead of diesel::insert.
    Pankaj Jangid
    @jangid
    Initial setup migration is allowed to be edited. The comments say that future changes will be included as migrations; so it is safe to edit 0000000...diesel_initial_setup. I am just curious, how those future migrations will be generated? which command to execute to generate those migrations.