Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    CarlyRaeJepsenStan
    @CarlyRaeJepsenStan
    Oh well
    codedmart
    @codedmart:matrix.org
    [m]
    Nevermind I see
    Oh wait I am not sure what is happening.
    codedmart
    @codedmart:matrix.org
    [m]
    Question still stands. Can I turn off unused_imports warnings for schema.rs?
    codedmart
    @codedmart:matrix.org
    [m]
    Hmm ok so I added a src/schema.patch and added patch_file = "src/schema.patch" but I see an error when I try to run the diesel cli No such file or directory (os error 2).
    Georg Semmler
    @weiznich
    That error sounds like you miss the patch tool. About turning off the unused_imports lint: That works like any other lint. You put a #[allow(unused_imports)] on the corresponding item and the warning will disappear. Note that this also works as outer or inner attribute for a module.
    codedmart
    @codedmart:matrix.org
    [m]
    Yeah my bad 🤦
    Jovons
    @Jovons

    @Jovons It's impossible to say what exactly is wrong with your implementation without knowing details about the implementation. Either include more details or have a look at the corresponding test case.

    I tried the same custom_types as in the link you mentioned. But it still gives the same error after running cargo test. And I also pushed the code I have here: https://github.com/Jovons/diesel_demo/blob/master/tests/custom_types.rs

    29 replies
    vidyli
    @vidyli
    any example abourt using uuid mysql8 ?
    Georg Semmler
    @weiznich
    Mysql has no builtin uuid type, therefore diesel does not provide a mapping for rust uuid's.
    vidyli
    @vidyli
    as this article suggests, https://mysqlserverteam.com/mysql-8-0-uuid-support/, can we create a custom mysql type ?
    Georg Semmler
    @weiznich
    As this is a custom type that requires that you roll your own implementation implementing diesels traits.
    vidyli
    @vidyli
    @weiznich any guide I can refer, please?
    Georg Semmler
    @weiznich
    There is no need to ping me. Also one example is literally linked in the post above yours, so maybe take same time and read the documentation first?
    vidyli
    @vidyli
    thanks.
    Wagner
    @mwnDK1402

    In MySQL you can do:

    INSERT INTO table (column_list)
    VALUES (value_list)
    ON DUPLICATE KEY UPDATE
       c1 = v1, 
       c2 = v2,
       ...;

    And in PostgreSQL you can do:

    INSERT INTO the_table (id, column_1, column_2) 
    VALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')
    ON CONFLICT (id) DO UPDATE 
      SET column_1 = excluded.column_1, 
          column_2 = excluded.column_2;

    How can you do this in Diesel?

    Reading about this, it seems like it was unsafe in some way to do this before they implemented the ON CONFLICT clause in PostgreSQL: https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql

    So I'm hoping it can be done "atomically" with a single query.

    Still haven't found the MySQL equivalent.
    Wagner
    @mwnDK1402

    https://docs.diesel.rs/1.4.x/diesel/fn.insert_or_ignore_into.html
    Looks like PostgreSQL support is better in quite a few ways. Seems like this feature is implemented for PostgreSQL but not MySQL. I can't switch to PostgreSQL, but I will in future projects.

    For now, I think I'll do two queries:

    • insert_or_ignore_into
    • update
    2 replies
    codedmart
    @codedmart:matrix.org
    [m]
    Is there a good example I can follow to setup a trait or fn for something like a common column that all my tables have? For instance I have an updated_at column on most tables. So I would like to wrap diesel::update with my own update that just sets the updated_at column and returns the query so I can do other filtering or setting.
    codedmart
    @codedmart:matrix.org
    [m]
    Can you only call set once with query_builder?
    Oh nevermind I see
    Georg Semmler
    @weiznich
    @codedmart:matrix.org It is possible to write generic code wrapping diesels operations into custom functions, but doing that is not recommend for nearly all use cases. For setting the updated_at column you can use the trigger provided by the default migration.
    codedmart
    @codedmart:matrix.org
    [m]
    OK thanks!
    codedmart
    @codedmart:matrix.org
    [m]
    I just want to be sure I didn't miss something. Currently there is no way to log the queries that diesel runs right? I feel like I saw open discussions, but nothing implemented yet.
    Georg Semmler
    @weiznich
    At least on way to do that without implementing a custom connection on top of diesel, which does to logging internally. I think there are a few third party crates on crates.io providing this functionality.
    codedmart
    @codedmart:matrix.org
    [m]
    I am not sure what type to use for Numeric? I have that in my schema.rs, but I can't use Numeric or BigDecimal because of Serialize/Deserialize issues.
    codedmart
    @codedmart:matrix.org
    [m]
    I see what was missing. "serde" feature on bigdecimal.
    Nathan Loisel
    @nathanloisel
    Hey !
    Is there any plan to support batch insertion for Postgres using COPY IN STDIN? I think it is not possible with MySQL and SQLite.
    I know that rust-postgres support it : https://github.com/sfackler/rust-postgres/blob/master/tokio-postgres/tests/test/main.rs#L479
    Georg Semmler
    @weiznich
    There are no plans by the contributor team to add this any time soon as far as I'm aware. That written if someone submits a feature proposal we can discuss the design and will probably accept that feature as contribution later on.
    Nathan Loisel
    @nathanloisel
    If I find the time, I will investigate how this can be implemented with Diesel and create a feature proposal issue to discuss about it.
    Leonardo Javier Belen
    @leobelen

    Hi everyone, I am getting a weird error: the trait bound uuid::Uuid: diesel::Expression is not satisfied while #[derive(Insertable...)] on diesel 1.4.4 (with features="uuid") and uuid = "0.6.5" on a table definition with a simple table defined as #[derive(Insertable, Queryable, Identifiable, SimpleObject, Debug, Clone)]

    [table_name = "user"]

    [primary_key(id)]

    pub struct User {
    pub id: uuid::Uuid,
    pub name: String,
    pub age: i32,
    }
    is there something I am missing?

    1 reply
    I am on rust 1.51.0 on a catalina mac... and the github forum does not say much about the issue... but I consider that I am missing something but I am not sure what...
    buckuppo
    @buckuppo:matrix.org
    [m]

    Hi guys, I've been pulling my hair out trying to figure out how to do what I imagine is a simple query filtering rows that were created this month. This sql is what i would like to replicate in correct diesel syntax:

    select * from my_table where extract (month from postdate) = extract (month from CURRENT_DATE);

    Im trying: my_table.filter(postdate.month().eq(Chrono::Utc::now().month()))

    Error is that postdate which is type timestamptz has no method month(). Definitely would appreciate a any help. It's my first swing at building a web service so apologies in advance for any ignorance.

    2 replies
    Alexx Roche
    @alexxroche
    I'm finding diesel setup a little inscrutable. (Not sure what I'm doing wrong.) "Unable to open the database file"
    strace and postgres logging show that diesel isn't even attempting a DB connection.
    Georg Semmler
    @weiznich
    @alexxroche That sounds like you've used something that got interpreted as sqlite database url. Otherwise see diesel-rs/diesel#2646 for potential improvements for the error message. Contributions are welcome there.
    Alexx Roche
    @alexxroche
    But psql $(grep DATABASE_URL .env |awk -F'DATABASE_URL=' '{print $2}') connects just fine.
    diesel seems to be correctly collecting the database-url from .env Creating database: postgres://prwad:prwad@127.0.0.1:5432/prwad?sslmode=disablebut then does nothing with it.
    Georg Semmler
    @weiznich
    Are you sure that you compiled diesel_cli with postgres support enabled, because connecting to databases using postgres url's is something that we obviously test in our ci :wink:.
    Alexx Roche
    @alexxroche
    I'll run cargo install diesel_cli --no-default-features --features "postgres sqlite mysql" and let you know if you just became my hero of the day.
    Alexx Roche
    @alexxroche
    @weiznich Thank you! cargo was having some linker failure that was preventing it compiling in pg support.
    Now I need to track down why my system was throwing: Compiling diesel_cli v1.4.1 error: linking withccfailed: exit code: 1 (but I feel that isn't a diesel issue.)
    Georg Semmler
    @weiznich
    @alexxroche That means rustc cannot find a compatible version of libpq
    1 reply
    Wagner
    @mwnDK1402

    I'm running a complicated, raw SQL query using sql_query. I'll post it as a reply to this thread.

    It works when I'm running it locally. I'm running it in a Rocket unit test on Windows, connected to a local MySQL database.

    When I deploy it on AWS Lambda, it gives me an SQL error: Using unsupported buffer type: 245 (parameter: 1)
    When deployed, it connects to an AWS RDS MySQL database.

    I'm not sure how to start troubleshooting this, since the query does run on both MySQL servers if I substitute the ? parameter with a literal. I would like some help understanding what could cause this difference, and also understanding what the error means.

    The closest I could find was this: https://forums.mysql.com/read.php?168,242330,242330
    But that's a different error, with no responses.

    40 replies
    Cobalt
    @Chaostheorie
    Is there still support for custom types with enums? The example found in the docs is outdated and I somehow run into problems with Expression when I'm trying to do the same as JSONB. 'm pretty sure my implementation has one too many To/ FromSQl implementations:
    use diesel::*;
    use diesel::{
        deserialize::{FromSql, Result as DResult},
        pg::Pg,
        serialize::{IsNull, Output, Result as SResult, ToSql},
    };
    use serde::{Deserialize, Serialize};
    use std::io::Write;
    
    #[derive(Debug, Clone, Serialize, Deserialize, AsExpression, PartialEq)]
    #[diesel(not_sized)]
    pub struct ShortTextElement {
        pub name: String,
        pub value: Option<String>,
    }
    
    #[derive(Debug, Clone, Serialize, Deserialize, AsExpression, PartialEq)]
    #[diesel(not_sized)]
    pub struct TextElement {
        pub name: String,
    }
    
    #[derive(SqlType)]
    #[postgres(type_name = "Jsonb")]
    pub struct FormElement;
    
    #[derive(Debug, PartialEq, Serialize, Deserialize, FromSqlRow, AsExpression)]
    #[sql_type = "FormElement"]
    #[diesel(not_sized)]
    pub enum FormElementEnum {
        ShortTextElement(ShortTextElement),
        TextElement(TextElement),
    }
    
    /// Copied from diesel source for JSONB with slight variation with distinctive encoding
    impl ToSql<FormElement, Pg> for FormElementEnum {
        fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> SResult {
            out.write_all(&[2])?;
            serde_json::to_writer(out, self)
                .map(|_| IsNull::No)
                .map_err(Into::into)
        }
    }
    
    /// Copied from diesel source for JSONB with slight variation with distinctive encoding
    impl ToSql<FormElementEnum, Pg> for FormElementEnum {
        fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> SResult {
            out.write_all(&[2])?;
            serde_json::to_writer(out, self)
                .map(|_| IsNull::No)
                .map_err(Into::into)
        }
    }
    
    /// Copied from diesel source for JSONB with slight variation with distinctive encoding
    impl FromSql<FormElement, Pg> for FormElementEnum {
        fn from_sql(nullable: Option<&[u8]>) -> DResult<Self> {
            let bytes = not_none!(nullable);
            if bytes[0] != 2 {
                return Err("Unsupported JSONB encoding version".into());
            }
            serde_json::from_slice(&bytes[1..]).map_err(|_| "Invalid Json".into())
        }
    }
    
    /// Copied from diesel source for JSONB with slight variation with distinctive encoding
    impl FromSql<FormElementEnum, Pg> for FormElementEnum {
        fn from_sql(nullable: Option<&[u8]>) -> DResult<Self> {
            let bytes = not_none!(nullable);
            if bytes[0] != 2 {
                return Err("Unsupported JSONB encoding version".into());
            }
            serde_json::from_slice(&bytes[1..]).map_err(|_| "Invalid Json".into())
        }
    }
    While this code compiles it can't be used as a type of an attribute with Insertable. It seems like he deriving from AsExpression either doesn't properly implement Expression or I'm donig something wrong: ```rust
    2 replies
    Georg Semmler
    @weiznich
    First of all to which documentation do you refer? Documentation published on our website is tested via the CI system, therefore I'm quite sure it's not outdated.
    It's not really documentation but the closest thing I've found