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
    Hello! Is it possible to delete from multiple tables in one query like this? If so, how?
    DELETE T1, T2
    FROM T1
    INNER JOIN T2 ON T1.key = T2.key
    WHERE condition;
    Georg Semmler
    @weiznich
    This is not supported by the built-in dsl.
    Wagner
    @mwnDK1402
    Alright, just gotta do that in multiple queries, then. Thanks.
    Wagner
    @mwnDK1402
    Here's my use case, by the way. If you have link tables, this is very useful. I don't know how I would do this in Diesel, so I'm just writing a prepared statement instead in raw SQL. I hope you consider adding this feature, if possible.
    DELETE exhibition, exhibition_ownership
    FROM exhibition
    INNER JOIN exhibition_ownership
    ON exhibition.id = exhibition_ownership.exhibition_id
    WHERE client_id = ?;
    It seems easier to write all my queries in raw SQL. Is that less performant than using the built-in DSL?
    Georg Semmler
    @weiznich
    Feel free to open a feature request to discuss the design and a followup PR adding the implementation. I'm not opposed in having this feature, it's just that this is something that I myself did not need yet so there is no real motivation for me to add this now, especially as there are other things I consider more important for my own projects.
    Wagner
    @mwnDK1402
    Alright, will do.
    Georg Semmler
    @weiznich

    It seems easier to write all my queries in raw SQL. Is that less performant than using the built-in DSL?

    It really depends on your usecase, but assuming you write the same query using raw SQL (via sql_query) and the built-in DSL, using the DSL should normally generate a more performant variant as we know much more about the query at compile time.

    Wagner
    @mwnDK1402
    Yeah, that's what I thought. Well, that's a shame. I'll use the built-in DSL when I can.
    Georg Semmler
    @weiznich
    There is no way to provide the same guarantees based on a user constructed string than one something that we could check on compile time.
    Jovons
    @Jovons
    Hi, I followed examples in ToSql and FromSql to use MyEnum in model struct. However, MyEnum cannot be used as a look-up criteria, due to : the trait diesel::Expression is not implemented for diesel_demo::models::MyEnum
        let results = posts
            .filter(published.eq(diesel_demo::models::MyEnum::A)) // cannot be used as a look-up criteria
            .limit(5)
            .load::<Post>(&connection)
            .expect("Error loading posts");
    Georg Semmler
    @weiznich
    @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.
    codedmart
    @codedmart:matrix.org
    [m]
    I am not sure I understand how modules work, more specifically with import_types and schema.rs. I have a file db/sql_types.rs that has Citext. I also have a db/mod.rs file that has pub mod sql_types in import_types in my diesel.toml I have ["diesel::sql_types::*", "crate::db::sql_types::*"]
    But I still see not found in scope for Citext in schema.rs
    Georg Semmler
    @weiznich
    It's hard to answer that without any details about the actual error message and maybe some code to reproduce the issue. How does your generated table! call look like for example?
    codedmart
    @codedmart:matrix.org
    [m]
    @weiznich: This was generated for one table: https://gist.github.com/codedmart/636a73fb82ecdbbf4f5a140eca27e77e
    Georg Semmler
    @weiznich
    That sounds like the command you used to generate that file did not pickup the import_types statement.
    Are you sure that you actually regenerated the schema.rs after you added the import_types statement to your diesel.toml
    codedmart
    @codedmart:matrix.org
    [m]
    Oh maybe not.
    codedmart
    @codedmart:matrix.org
    [m]
    @weiznich: Now I get this warning: unused import:crate::db::sql_types::Citext``?
    Georg Semmler
    @weiznich
    Could you clarify what's your question? Also you don't need to ping me every time…
    codedmart
    @codedmart:matrix.org
    [m]
    Oh sorry :).
    https://gist.github.com/codedmart/cc6533aea3b89b254ff159aeb71444b6 I see this now when I build? Can I turn off warn on unused imports for schema.rs?
    What does this guy mean by updating the client software?
    I think I'm just going to downgrade my Postgresql - this isn't an important project in any way that needs better authentication
    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.