A safe, extensible ORM and Query Builder for Rust – For feature requests and longer questions also see https://github.com/diesel-rs/diesel/discussions/categories/q-a
@Urhengulas I would probably just try to call
Connection::begin_test_connection()
for each connection on pool creation via ther2d2::CustomizeConnection
trait.
Generally speaking: I think a guide how to write tests involving diesel is certainly something that would be a welcome addition for our web page.
@weiznich Thanks. That sounds like an idea. If I come up with a good pattern I might send a description PR for the website :)
p::plate
.select(m::measurement.select(count_star()).filter(m::plate_id.eq(p::id)))
.load::<i64>(&conn)
is this supported? I get some errors https://bpa.st/YP3A
plate
with other tables and select several more columns
diesel::table! {
use crate::gametypes;
use diesel::sql_types::*;
players (uid) {
uid -> BigInt,
name -> Text,
address -> Text,
pos -> gametypes::PosType,
vital -> Array<Integer>,
indeath -> Bool,
}
}
#[derive(Debug, PartialEq, Queryable, Insertable, Associations, Identifiable)]
#[table_name = "players"]
#[primary_key(uid)]
pub struct PGPlayerWithID {
uid: i64,
name: String,
address: String,
pos: Position,
vital: Vec<i32>,
indeath: bool,
}
let pgplayer = players::table.filter(players::uid.eq(user.accid)).first::<PGPlayerWithID>(conn)?;
diesel::table! {
use crate::gametypes;
use diesel::sql_types::*;
players (uid) {
uid -> BigInt,
name -> Text,
address -> Text,
pos -> gametypes::PosType,
vital -> Array<Integer>,
indeath -> Bool,
}
}
#[derive(Debug, PartialEq, Queryable, Insertable, Associations, Identifiable)]
#[table_name = "players"]
#[primary_key(uid)]
pub struct PGPlayerWithID {
uid: i64,
name: String,
}
let pgplayer = PGPlayerWithID::select.filter(players::uid.eq(user.accid)).first::<PGPlayerWithID>(conn)?;
I think maybe for something like this rather that relying on a full blown query hierarchy it be better to just add a derive that builds a players::table.select((players.uid, players.name)) that the end user could simply just use the rest of regular diesel along with it
I have actually finished implementing support for the above in my ORM lib (on top of diesel) using rust macros. I am currently working on solving this issue for INSERT
and UPDATE
. And then will move on to associations
(I have a vague idea on how to solve the n+1 problem)
[u8; 8]
in a derive Queryable struct but not a derive Insertable struct (you get an error about it not implementing Expression for the latter). I've worked around it with a serialize_as wrapper for now. Should I file a github issue or is there an underlying cause here I haven't thought about?
#[macro_use]
extern crate diesel;
table! {
examples (id) {
id -> Int4,
bytes -> Bytea,
}
}
// Compiles
#[derive(Queryable)]
struct Example {
id: i32,
bytes: [u8; 8],
}
// Doesn't compile
#[derive(Insertable)]
#[table_name = "examples"]
struct NewExample {
bytes: [u8; 8],
}
for now, my code looks like:
table
.filter(available.eq(query.available()))
.limit(query.limit())
.offset(query.offset());
diesel is AWESOME~~~