Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    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
    Georg Semmler
    @weiznich
    This is also tested via CI and I can assure you that it is not outdated. (More like: It's a test case that uses an unreleased future version of diesel.)
    Cobalt
    @Chaostheorie
    Ah okay. It's just that PgValue didn't exist in my version. Seems like this was my error
    Georg Semmler
    @weiznich
    Instead implementing FromSql/ToSql manually you should forward inside of the implementation to the existing impl for serde_json::Values, otherwise you need to reimplement much of postgres binary protocol parsing yourself.
    Cobalt
    @Chaostheorie
    But isn't that what I'm doing with serde_json::from/ to_reader?
    Georg Semmler
    @weiznich
    No, that's not the thing you are doing. You don't call from_sql/to_sql on serde_json::Value internally.
    Other than that: You are missing two #[sql_type = "blub"] annotations for your #[derive(AsExperssion)] attributes.
    Cobalt
    @Chaostheorie
    The annotations are added though How would I go about working wiht serde_json::Value without parsing twice? My intention was to have an enum that can be used as a type in the models that already has a specific structure. So that I can guarantee all elements in the DB have this type and aren't 'just' Values?
    Or is there no way other than evaluating after initially parsing for type checking?
    Georg Semmler
    @weiznich
    impl ToSql<FormElement, Pg> for FormElementEnum {
        fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> SResult {
            <serde_json::Value as ToSql<Jsonb, Pg>::to_sql(&serde_json::to_value(self), out)
        }
    }
    and similarly for the FromSql impl.
    That written if you only insert/load FormElementEnum you only need to implement FromSql/ToSql for this type.
    Cobalt
    @Chaostheorie
    Thank you. Now I understand what you mean. I'm kinda new to the rust type system and this is still kinda confusing. I will try to understand it more in depth. Your explanation is really appreciated
    Eduardo Colina
    @eacolina
    Hey! I'm trying to apply a migration for a Postgres DB but I'm getting an error: Unexpected null for non-null column
    This is the up file
    -- Your SQL goes here
    CREATE UNIQUE INDEX tx_hash_index
        ON sol_transaction(tx_hash);
    
    CREATE TABLE order_transaction(
        order_id VARCHAR(255) NOT NULL ,
        tx_hash VARCHAR(255) UNIQUE NOT NULL,
        PRIMARY KEY(order_id, tx_hash),
        CONSTRAINT tx_hash_fk FOREIGN KEY(tx_hash)
        REFERENCES sol_transaction(tx_hash)
        ON DELETE CASCADE
    );
    And the referenced table was created in a earlier migration with:
    -- Your SQL goes here
    CREATE TABLE sol_transaction(
        id BIGSERIAL PRIMARY KEY,
        tx_hash VARCHAR(255) NOT NULL,
        from_address VARCHAR(255) NOT NULL,
        to_address VARCHAR(255) NOT NULL,
        VALUE DOUBLE PRECISION NOT NULL DEFAULT 0,
        BLOCK_HASH VARCHAR(255) NOT NULL,
        BLOCK_HEIGHT BIGINT NOT NULL,
        BLOCK_TIME TIMESTAMP  NOT NULL DEFAULT CURRENT_TIMESTAMP
    )