!lNGJpfiFVovXFJYmwx:matrix.org

Diesel

1731 Members
A safe, extensible ORM and Query Builder for Rust59 Servers

Load older messages


SenderMessageTime
8 Apr 2021
@jovons-6064122d6da037398478aacc:gitter.imJovons (Jovons)
error[E0277]: the trait bound MyEnum: diesel::Expression is not satisfied
--> tests\custom_types.rs:50:10
|
50 | #[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
| ^^^^^^^^^^ the trait diesel::Expression is not implemented for MyEnum
|
= note: required because of the requirements on the impl of diesel::expression::AsExpression<MyEnum> for MyEnum
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
09:23:22
@jovons-6064122d6da037398478aacc:gitter.imJovons (Jovons) above is the error 09:23:33
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich (Georg Semmler) https://github.com/Jovons/diesel_demo/blob/master/tests/custom_types.rs#L19 and https://github.com/Jovons/diesel_demo/blob/master/tests/custom_types.rs#L24 and https://github.com/Jovons/diesel_demo/blob/master/tests/custom_types.rs#L30 and https://github.com/Jovons/diesel_demo/blob/master/tests/custom_types.rs#L40 need to use the same sql type for obvious reasons. 09:24:39
@jovons-6064122d6da037398478aacc:gitter.imJovons (Jovons) Thank you George. The error is gone. Now I'm able to study the rest from here 09:30:27
@jovons-6064122d6da037398478aacc:gitter.imJovons (Jovons) * Thank you George. The error is gone. Now I'm able to study the rest from here 09:30:48
@jovons-6064122d6da037398478aacc:gitter.imJovons (Jovons)

This error message indicates that you did not add a valid #[sql_type = "SomeSqlTypeSupportedByMysql"] annotation.

Hi, how did you find the reason for that error message from the source code? I found, in rust, error message sometimes is very confusing. I was looking into code for AsExpression.

09:53:23
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich (Georg Semmler) In this case that's not that simple, you need to know quite a lot of things.
Specifically I know that #[derive(SqlType)] internally generates a NonNull impl, while the #[derive(AsExpression)] impl has a bound on NonNull for the generic argument of the trait. So if you pass in a #[sql_type = "String"] that type does not implement NonNull which then causes an error in the generated impl, that looks like impl AsExpression<String> for YourType {…}
09:57:36
@jovons-6064122d6da037398478aacc:gitter.imJovons (Jovons) I see. Thank you 10:48:55
@mwndk1402-6041fd046da0373984659668:gitter.immwnDK1402 (Wagner)

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.

10:49:45
@mwndk1402-6041fd046da0373984659668:gitter.immwnDK1402 (Wagner) Oh, I found this for PostgreSQL: https://docs.diesel.rs/1.4.x/diesel/pg/upsert/index.html 10:50:59
@mwndk1402-6041fd046da0373984659668:gitter.immwnDK1402 (Wagner) Still haven't found the MySQL equivalent. 10:51:14
@mwndk1402-6041fd046da0373984659668:gitter.immwnDK1402 (Wagner)

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
11:04:10
@codedmart:matrix.orgcodedmart 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. 12:27:05
@codedmart:matrix.orgcodedmartCan you only call set once with query_builder?13:16:41
@codedmart:matrix.orgcodedmartOh nevermind I see13:18:22
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich (Georg Semmler) @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. 14:46:45
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich (Georg Semmler) Diesel generally tries to not hide the differences between the different sql dialects. That's not by accident, but because we believe that's the only way to allow users to write performant code. 14:48:36
@codedmart:matrix.orgcodedmartOK thanks!15:02:02
@codedmart:matrix.orgcodedmartI 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.17:45:31
@mwndk1402-6041fd046da0373984659668:gitter.immwnDK1402 (Wagner) I definitely agree with that. My problem was that I couldn't find any support for the ON DUPLICATE KEY UPDATE clause in MySQL. The equivalent, ON CONFLICT, in PostgreSQL is implemented, though. 18:04:17
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich (Georg Semmler) 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. 18:07:36
@codedmart:matrix.orgcodedmartI 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.18:36:09
@codedmart:matrix.orgcodedmartI see what was missing. "serde" feature on bigdecimal.18:51:17
9 Apr 2021
@luciandesozay_jaexilgilroymaitre_pyrovermilion:matrix.orgjaexilmaitre_McCarthyDundon_KotaroMatsushima PramilaJayapal_MonaEltahawy_FrederikHahn_YvonneGreen_wwwscoochorg _gilmarbolla8_mandaeans AidiFulangxisi_parsi_GedungMerdeka_Expeditus_Desteni_Sikhs_Baha'i_Parsi_wwworientchurchva_Epoque_AwaFallMirone joined the room.07:07:31
@nathanloisel-571f5a69659847a7aff49254:gitter.imnathanloisel (Nathan Loisel) joined the room.09:29:42
@nathanloisel-571f5a69659847a7aff49254:gitter.imnathanloisel (Nathan Loisel) 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
09:29:43
@weiznich-55bcb4c20fc9f982beabc3ad:gitter.imweiznich (Georg Semmler) 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. 09:30:59
@nathanloisel-571f5a69659847a7aff49254:gitter.imnathanloisel (Nathan Loisel) 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. 09:34:50
10 Apr 2021
@luciandesozay_jaexilgilroymaitre_pyrovermilion:matrix.orgjaexilmaitre_McCarthyDundon_KotaroMatsushima PramilaJayapal_MonaEltahawy_FrederikHahn_YvonneGreen_wwwscoochorg _gilmarbolla8_mandaeans AidiFulangxisi_parsi_GedungMerdeka_Expeditus_Desteni_Sikhs_Baha'i_Parsi_wwworientchurchva_Epoque_AwaFallMirone left the room.01:36:38
@michael.:matrix.orgMichael Amoani changed their profile picture.12:47:13

There are no newer messages yet.


Back to Room List