!lNGJpfiFVovXFJYmwx:matrix.org

Diesel

3153 Members
A safe, extensible ORM and Query Builder for Rust79 Servers

Load older messages


SenderMessageTime
22 Apr 2024
@silence_zhpf:matrix.orgsilence_zhpf *

That means I can't define a function to return an UpdateStatement like this ?

    fn update_by_goods_id(
        id: &str,
    ) -> UpdateStatement<member_exchange_goods::table, Eq<member_exchange_goods::columns::id, String>>
    {
        use member_exchange_goods::dsl;
        diesel::update(dsl::member_exchange_goods.filter(dsl::goods_id.eq(id.to_string())))
    }

06:14:51
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznichYes that's not supported yet.06:15:16
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich(At least as long as there is no type(-def) for this in the public API)06:15:43
@silence_zhpf:matrix.orgsilence_zhpf So I can't reuse the update fragment like query fragment, that's right? 06:18:51
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznichI cannot give you a straight forward answer about that. It might be possible or it might be not possible. I would need to sitdown and read the documentation and try a few things but I don't have the capacity to do that anytime soon. That means you are on your own there.06:22:15
@silence_zhpf:matrix.orgsilence_zhpf Maybe I can use the Update helper_types, but I didn't know how, maybe I will solve it by myself in future. But anyway, thanks. 06:27:47
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich

That doesn't help as you don't call .set() in your function, but the definition of the typedef shows how you could get the correct type for the WhereClause via the corresponding public trait + the associated type there.

Essentially you could define your own type alias without the changes like that:

pub type Update2<Target, > = UpdateStatement<<Target as HasTable>::Table, <Target as IntoUpdateTarget>::WhereClause>;

and then just use the type of the expression that you passed to update(…) as Target type (should be something like dsl::Filter<member_exchange_goods::table, dsl::Eq<…>>)

06:37:53
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich I fear I cannot really help you there. I don't use windows for development purposes, but what I know is that the -msvc target is preferred there and the -gnu target is sometimes hard to use. So you really might want to consider using the other target. 07:16:46
@maksextraidiot:gitter.immaksextraidiot
In reply to @weiznich-55bcb4c20fc9f982beabc3ad:gitter.im
It's not supported in the built-in DSL. It might be possible to extend the DSL, but I cannot give a specific example for full join there.
Are there some plans about adding it?
15:34:08
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich There are no specific plans from my side to implement that anytime soon as that’s not a feature I personally needed in the past.
That written: we are often open to accept DSL extensions as PR if they represent commonly used syntax (part of the sql standard) and they are covered with tests and documentation.
16:04:20
23 Apr 2024
@dlk9999:gitter.imdlk9999 joined the room.22:58:34
@dlk9999:gitter.imdlk9999Brand newbie here. I have (seemingly successfully) intalled diesel_cli in my Linux system, but my terminal does not seem to find the diesel command. Any ideas welcome.23:00:15
24 Apr 2024
@oeed-5590d87115522ed4b3e2ff94:gitter.imoeed (Oliver Cooper)

Json deserialized values that implement FromSql<Json, _> don't seem to automatically implement Queryable, while Option<T> of the same type does. Is this intended or just unfortunately necessary? Just a little confusing trying to debug why.

For example:

use diesel::{
    deserialize::FromSql,
    mysql::{Mysql, MysqlValue},
    prelude::*,
    sql_types, AsExpression,
};
use serde::Deserialize;

table! {
  example_table (id) {
    id -> Unsigned<Bigint>,
    nullable -> Nullable<Json>,
    non_nullable -> Json,
  }
}

#[derive(Debug, Deserialize, AsExpression)]
#[diesel(sql_type = sql_types::Json)]
struct MyJsonValue {
    hello: String,
}

impl FromSql<sql_types::Json, Mysql> for MyJsonValue {
    fn from_sql(value: MysqlValue<'_>) -> diesel::deserialize::Result<Self> {
        let result = <serde_json::Value as FromSql<sql_types::Json, Mysql>>::from_sql(value)?;
        Ok(serde_json::from_value(result)?)
    }
}

fn select_nullable(conn: &mut MysqlConnection) {
    let result: Option<MyJsonValue> = example_table::table
        .select(example_table::nullable)
        .first(conn)
        .unwrap(); // compiles fine
}

fn select_non_nullable(conn: &mut MysqlConnection) {
    let result: MyJsonValue = example_table::table
        .select(example_table::non_nullable)
        .first(conn) // fails with: the trait `Queryable<diesel::sql_types::Json, Mysql>` is not implemented for `MyJsonValue`
        .unwrap();
}

After some type tracking I discovered it's because Option<T> implements Queryable where Option<T>: FromSql, but the same is not true for just T. Is perhaps another broad Queryable implementation that would cover this case? i.e. in the above, the issue is solved with:

impl Queryable<sql_types::Json, Mysql> for MyJsonValue {
    type Row = MyJsonValue;

    fn build(row: Self::Row) -> deserialize::Result<Self> {
        Ok(row)
    }
}
00:58:02
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich That sounds like the target directory for cargo install is not part of your PATH environment variable. You might need to add it. (Given that we don't know how you installed rust, it's hard to say which directory needs to be added). 05:31:17
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich Seems like you missed to derive #[derive(FromSqlRow)] for your MyJsonValue type. That is required for loading values from the database. 05:31:57
@oeed-5590d87115522ed4b3e2ff94:gitter.imoeed (Oliver Cooper)Ah nice thank you, didn't realise that was required07:15:22
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich

Do you have suggestions how to make that more clear. The documentation of FromSql already states:

Types which implement this trait should also have #[derive(FromSqlRow)]

07:18:36
@silence_zhpf:matrix.orgsilence_zhpf I have a question about the into_boxed: the result of into_boxed didn't imply the Clone trait? 08:03:01
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznichNo that's not supported as trait objects cannot be clone.08:07:55
@silence_zhpf:matrix.orgsilence_zhpfthanks...08:13:25
@kartiksoneji_gitlab:gitter.imKartik Soneji joined the room.10:15:25
@diana96alazzam:gitter.imDiana AlazzamHello I'm trying to order results from my query according to a search keyword position in the item name or description. From what I understand, INSTR() is not provided by diesel, so I think I need to use raw sql. This might be dumb question, but is there a way to use raw sql for parts of the query? because I have already wrote another filter logic for the same function and I don't want to change everything just for this search thing. Any thoughts? 12:49:45
@silence_zhpf:matrix.orgsilence_zhpf

How can I define a type this code

dsl::qty.eq(dsl::qty-1)
13:01:42
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznichI don't think we provide support for describing the return type of a numeric operation yet. PR's for that are welcome.13:07:08
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich You might want to use diesel::dsl::sql for that or you might be able to define the DSL for your function via diesel::sql_function! 13:07:45
@kartiksoneji_gitlab:gitter.imKartik Soneji Hi, I'm using diesel 1.4.8 with postgres on MSVC nightly-2024-02-04, and the program seems to be crashing when a diesel model is returned from a function with (exit code: 0xc0000374, STATUS_HEAP_CORRUPTION).
Interestingly, this doesn't happen if I use a local instance instead of a remote one.
14:18:10
@kartiksoneji_gitlab:gitter.imKartik SonejiI'm not very well versed with rust, and am a bit unsure how to begin to debug this.14:18:35
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich

Do you use an ssl connection for the remote instance? There was a libpq bug in versions prior to 16.2 to might cause issues with newer OpenSSL versions.

That written: diesel 1.4.8 is out of support for some years now. It might also be that updating to 2.1 will fix the problem. (Although that will likely require quite a few code changes)

14:36:31
@diana96alazzam:gitter.imDiana AlazzamThanks 14:40:07
@kartiksoneji_gitlab:gitter.imKartik SonejiInteresting, but why would an OpenSSL bug cause the crash only when models are returned from a function? Printing the response shows the query does work.14:50:50

Show newer messages


Back to Room ListRoom Version: