1049 Members
A safe, extensible ORM and Query Builder for Rust43 Servers

Load older messages

Timestamp Message
13 Nov 2019
10:52:43@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter)

@weiznich I have a bunch models for which I just need to do simple CRUD operations, so I would like to write trait that defines those methods and then just implement it for my models, something like this:

pub trait Create: Comment {
    fn create(self, pool: DBPool) -> Result<usize, Error> {
        let conn = pool.get()?;
        let result = diesel::insert_into(comments::table)

The problem is that I need to write the trait for each different model, while I would like to write it once and reuse it for any model. What should be the trait bounds I should declare so that it works for any Insertable, Changeset or Queryable?

10:59:41@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter) (edited) ... bounds I should declare so that it works for any `Insertable`, `Changeset` or `Queryable`? => ... bounds to make it generic? It is a pity it is so difficult to reuse code
11:04:02@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter)Maybe there is also a nicer and more clever way of doing this
11:50:41@gitter_weiznich:matrix.orgGeorg Semmler (Gitter) @Figments What's the error message? It's quite hard to guess what's wrong without knowing the full code there :wink:
11:54:17@gitter_weiznich:matrix.orgGeorg Semmler (Gitter)

@codearm_gitlab I do not see why you would need a Create trait for each struct? Something like

trait Create {
     fn create(self, pool: DBPool) -> Result<usize, Error>;

can easily be implemented for all types you want.
Additionally: Can you explain what code you would reuse exactly?

11:55:12@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter) @weiznich Yeah, I could do that, but then I have to write
11:56:43@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter)

@weiznich I could do that, but then I would have to write

 let conn = pool.get()?;
 let result = diesel::insert_into(comments::table)

For each one of them, I know, it is not much but it is not very good practice to have duplicated code around

11:58:10@gitter_weiznich:matrix.orgGeorg Semmler (Gitter) The only duplicated code there is let conn = pool.get()?. Depending on your exact usecase this can easily be done generically outside of the function by for example just taking a connection instead of a pool there.
11:59:55@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter) Well, yeah, I can remove it, no prob, but for the part of insert_into and so, it would be nice if I could pass the arguments as parameters to the trait method or something
12:01:13@gitter_weiznich:matrix.orgGeorg Semmler (Gitter) To repeat that again: That's not duplicated code. Yes you cant write the information there in a different order or using on function call instead of a building pattern, but you need to specify those information at some point.
12:03:11@gitter_codearm_gitlab:matrix.orgAitor Ruano (Gitter)Okay!
12:31:32@gitter_figments:matrix.orgLandon Mote (Gitter) @weiznich All I've been able to get out of it is that it's a Deserialization Error (after brute-forcing the Error enum and checking each and every variant)
12:31:51@gitter_figments:matrix.orgLandon Mote (Gitter)I know nothing beyond that
12:32:26@gitter_weiznich:matrix.orgGeorg Semmler (Gitter)Can you post the exact error message?
12:34:52@gitter_figments:matrix.orgLandon Mote (Gitter)One sec
12:35:31@gitter_figments:matrix.orgLandon Mote (Gitter)
Unexpected null for non-null column
12:37:20@gitter_figments:matrix.orgLandon Mote (Gitter)...huh
12:37:24@gitter_figments:matrix.orgLandon Mote (Gitter)Welp
12:37:29@gitter_figments:matrix.orgLandon Mote (Gitter) I should've checked that to begin with lmao
12:37:48@gitter_figments:matrix.orgLandon Mote (Gitter) One column was meant to be null, I just forgot to set it as null in the table declaration
12:37:55@gitter_weiznich:matrix.orgGeorg Semmler (Gitter):+1:
12:38:01@gitter_figments:matrix.orgLandon Mote (Gitter) (edited) ... as null in ... => ... as nullable in ...
12:38:05@gitter_weiznich:matrix.orgGeorg Semmler (Gitter)Was about to saying exactly that :wink:
12:38:06@gitter_figments:matrix.orgLandon Mote (Gitter) Sorry for botherin'
12:38:27@gitter_weiznich:matrix.orgGeorg Semmler (Gitter)No worries. Asking questions is never a mistake.
12:38:28@gitter_figments:matrix.orgLandon Mote (Gitter)me and my dumb brain lmao
12:40:47@gitter_figments:matrix.orgLandon Mote (Gitter)Thanks a ton, Georg
21:58:42@gitter_theduke:matrix.orgtheduke (Gitter) hey @weiznich , are you around?
22:00:59@gitter_weiznich:matrix.orgGeorg Semmler (Gitter) @theduke Yes
22:04:27@gitter_theduke:matrix.orgtheduke (Gitter) cool, I'm taking this to a private message since it concerns juniper

There are no newer messages yet.

Back to Room List