Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    HarmoGlace
    @zaitara:matrix.org
    [m]
    Does diesel support postgres composite type?
    Georg Semmler
    @weiznich
    @zaitara:matrix.org Yes. See the documentation for the record type for details.
    Boscop
    @Boscop
    It seems sqlite-bundled only applies to diesel_cli. So which sqlite dll does the diesel library call? (At which path)
    Georg Semmler
    @weiznich
    @Boscop Diesel calls whichever sqlite dll the linker provides at runtime. That is something that really depends on your environment.
    Boscop
    @Boscop
    @weiznich Ah right, I found it again.. Does it need a specific version of sqlite? Or can I always use the latest release's dll?
    Georg Semmler
    @weiznich
    It's fine to use whatever sqlite version you like as long as all required symbols are provided by that library. I think everything newer than 3.7 or something like that should be fine.
    Boscop
    @Boscop
    ok thanks
    Mathijs van Veluw
    @BlackDex
    Hello there, i'm trying to get the server version by running this: match diesel::sql_query("SELECT version() AS version;").get_result::<String>(conn).ok() {
    But, it doesn't work. I need to make a struct for this. Is there a way to not need this?
    Mathijs van Veluw
    @BlackDex
    With a bit of delay... But it keeps telling me it doesn't work, and i need to create a struct for this to work. Is there a way to just get the output as a string/text?
    Georg Semmler
    @weiznich
    @BlackDex sql_query requires always a custom struct that implements QueryableByName. Instead of using sql_query you could define your version function via sql_function! and use the typed query builder (so diesel::select(version()) which allows you to select directly into a String.
    That's because we can check at compile time that the later variant returns exactly one column/field, while we don't know that for the first one.
    Mathijs van Veluw
    @BlackDex
    Ah, thought it had to be something like that.
    Thx for the explanation. I will try the ::select()
    Jack
    @Jackbaude
    What is the best way to find unique fields that arent the primary key?
    Georg Semmler
    @weiznich
    Just filter by those fields?
    Leonora Tindall
    @NoraCodes
    Hello! A quick question I can't find in the docs - how does one run a Diesel migration not in a transaction? (In this case, CREATE INDEX CONCURRENTLY)
    Georg Semmler
    @weiznich
    @NoraCodes That's something that is not supported yet on any released diesel version. The next feature release will contain larger changes to the migration system, that allow configuring if a migration should be run in a transaction or not. (And may other things. See diesel-rs/diesel#2657 for details)
    Leonora Tindall
    @NoraCodes
    Okay. We're using Diesel in production, and as a work around we've created the index on the production database manually; should we wait for that issue to be resolved to bring the migration state up to the real state, or is there a workaround to record that we've run the migration manually? (other than just modifying the migration table ourselves)
    Dessalines
    @happydooby:matrix.org
    [m]
    Whats the case for creating an index not as a migration? In lemmy we do have a case where we want to re-index, so we run those in a job scheduler in our code using sql_query.
    Leonora Tindall
    @NoraCodes
    Ideally we wanted to have the database state fully captured by migrations; in fact this would be only the third migration of this service ever in 3 years of operating. It's not that we wanted to rebuild the index, but actually create a new one while the service is up, without disruption.
    BinAlyan
    @BinAlyan
    Is there some method like on_conflict() that can be used for foreign key constraints? (e.g: the row referenced by foreign key does not exist). When using on conflict I get the following error: "there is no unique or exclusion constraint matching the ON CONFLICT specification"
    Georg Semmler
    @weiznich
    No there isn't such a method as far as I'm aware. How would that work, without having a unique constraint somewhere? How would the database decide if there is a conflict or not?
    BinAlyan
    @BinAlyan
    Thank you, I am not really sure, but the postgresql yells at me if the row referenced by foreign key does not exist so it should somehow know? So I just want to null the foreign key to avoid the conflict and silence postegresql.
    Georg Semmler
    @weiznich
    I does know if it doesn't exist, but it's totally fine to have multiple columns referring to the same foreign key .
    BinAlyan
    @BinAlyan
    I meant in case the referenced row was deleted, so it seems in PostgreSQL foreign key must refer to existing row
    Georg Semmler
    @weiznich
    That's then not a unique or exclusion constraint, but a foreign key constrain. See the postgres documentation for details about the different constrain types.
    There is really not much to add here otherwise. I do not see what else you expect to hear from me other than: That's how postgres decided to implement this stuff, which is something that is completely out of diesels control.
    BinAlyan
    @BinAlyan
    Sorry I am not really familiar with databases, so there is no way to check for conflicts on foreign key constraints?
    Georg Semmler
    @weiznich
    Not that I'm aware of.
    BinAlyan
    @BinAlyan
    Thank you!
    Wagner
    @mwnDK1402
    image.png
    Sorry for bothering you again, @weiznich.
    I'm wondering if what I'm trying to do here is even possible.
    Georg Semmler
    @weiznich
    Don't ping me to ask questions. I'm not your personal support person. And please don't post "random" images here. Try to formulate complete questions of what you are trying to do and what you've already tried and why this did fail.
    Wagner
    @mwnDK1402
    Sorry, I'm not used to Gitter, and I'm in the middle of writing the actual question.
    I've just migrated my table from having one long JSON string to splitting that up into multiple tables, and for now I need some backwards compatibility, so I wrote a MySQL query to join the tables back into a JSON string.
    So my understanding was that I could write an arbitrary query like this, bind a parameter, so that I can choose which user's data to get, and load the string into a struct deriving from QueryableByName. That seems so be what's in the example, at least.
    Wagner
    @mwnDK1402
    But I can't really understand the error message.
    Georg Semmler
    @weiznich
    Again: Please include the necessary information in your question, not in some image. I cannot see an error message in your question.
    Wagner
    @mwnDK1402
    the trait bound `query_builder::sql_query::UncheckedBind<SqlQuery, u32, diesel::sql_types::Unsigned<diesel::sql_types::Integer>>: LoadQuery<_, Exhibitions>` is not satisfied
    the following implementations were found:
      <query_builder::sql_query::UncheckedBind<Query, Value, ST> as LoadQuery<Conn, T>>
    Georg Semmler
    @weiznich
    That says that there is some type missmatch somewhere in your query. I cannot tell you where as I don't know your code.
    Wagner
    @mwnDK1402
    #[derive(QueryableByName)]
    struct Exhibitions {
        #[sql_type = "String"]
        exhibitions: String,
    }
    
    #[post("/sqlconnect/readExhibitions.php", data = "<form>")]
    pub fn get_handler(
        form: Form<GetHandlerForm>,
        conn: Database,
    ) -> Result<JsonValue, Custom<String>> {
        Ok(
            match sql_query(include_str!("select_exhibitions.sql"))
                .bind::<Unsigned<Integer>, _>(form.cid)
                .load::<Exhibitions>(&*conn)
                .map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
                .pop()
                .map(|w| w.exhibitions)
            {
                Some(json) => serde_json::to_value(json)
                    .map_err(|e| {
                        Custom(
                            Status::InternalServerError,
                            format!("Failed to serialize Exhibitions: {}", e.to_string()),
                        )
                    })?
                    .into(),
                None => json!({
                    "exhibitions": []
                }),
            },
        )
    }
    It's at the call to load. Since Exhibitions derives from QueryableByName, I thought I could load it from the query, but something is wrong.
    Also, the exhibitions member of the struct refers to the exhibitions column in the table returned by my custom query. And that's a big JSON_OBJECT.
    Georg Semmler
    @weiznich
    #[sql_type="…"] expects a sql type not a rust type. Normally that should be a type from diesel::sql_types or a custom defined sql type.
    Wagner
    @mwnDK1402
    #[sql_type = "sql_types::Longtext"]
    exhibitions: String,
    Thank you so much!
    joonnna
    @joonnna

    Is it possible to conditionally perform update on conflicts with multiple values? (I'm using postrgres)

    Something like this:

    #[derive(Insertable)]
    struct InsertPost {
        id: i64,
        date: NaiveDate,
    }
    
    let insert_vec = vec![Test{id: 1, date: "2014.05.05"}, Test{id: 2, date: "2013.03.05"}];
    
    diesel::insert_into(tests)
      .values(&insert_vec)
      .on_conflict(id)
      .do_update()
      .filter(date.ge(excluded(date)))
      .set(date.eq(excluded(date)));

    I want to insert multiple values and only update the rows that meet a certain criteria, is there any way to acheive this?
    The example above does not compile as the type IncompleteDoUpdate type does not contain a filter method.

    Georg Semmler
    @weiznich
    @joonnna That's not possible as far as I'm aware of.
    joonnna
    @joonnna
    Thanks for the clarification!
    Diógenes Falcão
    @diogenes

    Hello folks,

    I'm trying to do a simple sum in a Postgres BIGINT (NOT NULL) column:

    orders.select(sum(units))
                    .filter(user_id.eq(&p_user_id))
                    .first(&connection).unwrap()

    (Where orders is the table, units is the BIGINT column and user_id is just a filter.)

    When I try to assign it to an i64 variable it throws:

    60 |             .first(&connection).unwrap();
       |              ^^^^^ the trait `diesel::deserialize::FromSql<diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::pg::Pg>` is not implemented for `i64`
       |
       = help: the following implementations were found:
                 <i64 as diesel::deserialize::FromSql<diesel::sql_types::BigInt, DB>>

    When I try to assign to a BigInt variable it throws:

    .first(&connection).unwrap();
       |              ^^^^^ the trait `diesel::Queryable<diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::pg::Pg>` is not implemented for `diesel::sql_types::BigInt`
       |
       = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<diesel::PgConnection, diesel::sql_types::BigInt>`

    Similar errors happen when I try to wrap the type using an Option (Option<i64> or Option<BigInt> var).

    Could you please help me to identify what's wrong here?

    Georg Semmler
    @weiznich
    A sum over a column of the type BigInt returns a Numeric that can by null. Therefore you need to use a compatible rust type like BigDecimal.