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
The function the API does provide me with is eq_all() but I'm not sure that's what I'm supposed to use as I'm unable to find any reference to it in any of the documentation. Using Diesel 1.4.8.
This is still the case after attempting to compile.
implicitly returns `()` as its body has no tail or `return` expression
I don't understand why I don't have access to this basic functionality during update(). I can insert_into() and call get_result() as expected, but not much else.
get_result()
was enough to return the struct I needed. I added unwrap()
and was able to compile. I'm also unsure if the errors I'm dealing with are the result of rust-analyzer
, discrepancies with documentation and the version I'm using (diesel: 1.4.8
, rustc: 1.64.0-nightly
), or with errors regarding existing code that should be discarded altogether. I wish I could figure this out while I'm writing a question, but these issues are only made apparent after I've solved the problem.
There is a problem while installing diesel cli with the specific version. we are stuck to find where to debug the problem. Can you guys please help us to get out of the situaiton? the command we provide to install,
cargo +nightly-2020-11-15 install diesel_cli --no-default-features --features mysql --verbose
the error we are facing is,
failed to compile `diesel_cli v1.4.1`, intermediate artifacts can be found at `/tmp/cargo-installU7viiO`
Caused by:
failed to parse manifest at `/usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.143/Cargo.toml`
Caused by:
feature `resolver` is required
consider adding `cargo-features = ["resolver"]` to the manifest
serde
via cargo update -p serde --precise=some.older.version
diesel::insert_into(address::table)
.values(&addresses)
// NOTE: this ON CONFILCT DO UPDATE do not change anything but
// is a trick to use SELECT OR INSERT in one call
.on_conflict((address::columns::location, address::columns::chain_id))
.do_update()
.set(address::columns::location.eq(excluded(address::columns::location)))
.get_results(db_conn);
addresses
is a Vec<_>
I wanted to create SELCT OR INSERT (what i implemented as upsert) . Is there a better way than update some filed to the same value? Vec<_>
from addressess
variable? Hey, I'm using diesel 2.0.0-rc.1, and I'm trying to use a union in a join, but I'm getting a build error.
// user_query and team_query are select expressions that return the same field
let authorized_files = user_query.union(team_query);
user_files::table.inner_join(authorized_files.on(user_files::id.eq(file_authorizations::user_file_id)))
.select(user_files::all_columns)
.get_results::<Self>(db).map_err(Error::from)
The error I'm getting is: the method
onexists for struct [VERY LONG TYPE] but its trait bounds were not satisfied
I also tried to select, or filter on the union, but it won't build. Loading the result of the union works though.
Does anyone know why isn't isn't working?
+----+---------+--------+---------------------+--------------+-----------+-------+
| id | title | author | created | last_updated | content | name |
+----+---------+--------+---------------------+--------------+-----------+-------+
| 30 | Title 1 | Dennis | 2022-08-10 19:13:32 | NULL | Content 1 | tag 1 |
| 30 | Title 1 | Dennis | 2022-08-10 19:13:32 | NULL | Content 1 | tag 2 |
+----+---------+--------+---------------------+--------------+-----------+-------+
All the tables are joinable! in schema.rs and I am able to get results with this
let all: Vec<(i32, String, String, Option<String>, i32, i32)> =
blog::table.inner_join(blog_tags::table)
.select((blog::id, blog::title, blog::author, blog::content, blog_tags::blog_id, blog_tags::tag_id))
.load(c).expect("Error");
but I am not sure how to load all of the "name" column into a vec (or array?) and store it in my "all" variable.
diesel::associations
for this. https://docs.diesel.rs/1.4.x/diesel_derives/derive.Associations.html
group_concat
will give you a String
instead of a Vec
of something, which might not be what you want.
Hello! I'm trying to create a struct where one item is of type Bytea
.
The struct is defined like this:
#[derive(
Debug,
Serialize,
Deserialize,
Clone,
Queryable,
Insertable,
Associations,
AsChangeset,
PartialEq,
)]
pub struct Model {
pub peer_id: Vec<u8>,
pub addr: String,
pub avail: i32,
pub max: i32,
pub files: i32,
}
I get the error:
the trait `diesel::Expression` is not implemented for `Vec<u8>`
schema.rs
file and information about your diesel version?I recently upgraded the clippy and rust
and now I'm getting clippy error for Insertable
error: this lifetime isn't used in the impl
--> diesel_schema/src/planner/attribute.rs:33:21
|
33 | #[derive(Serialize, Insertable)]
any quick suggestion - coming back to rust after 3-4 months - so may be I'm missing something basic thing
#[derive(Serialize, Insertable)]
#[table_name = "attributes"]
pub struct NewAttribute {
pub display_text: String,
pub description: Option<String>,
pub data_type: AttributeValueDataType,
}
Hi - I'm trying to deserialize some numeric
fields from postgresql to always have a fixed precision of 7
I'm using bigdecimal
, have created a custom type RoundedBigDecimal
and am using #[diesel(deserialize_as = "RoundedBigDecimal")]
The target struct derives Queryable (#[derive(Identifiable, Queryable)]
)
However I get a rustc
error: ^^^^ the trait Queryable<diesel::sql_types::Numeric, Pg> is not implemented for RoundedBigDecimal
a) is this a sensible approach and b) how can I satisfy this trait?
struct RoundedBigDecimal(BigDecimal);
impl Into<BigDecimal> for RoundedBigDecimal {
fn into(self) -> BigDecimal {
self.0
}
}
impl<DB, ST> FromSql<ST, DB> for RoundedBigDecimal
where
DB: Backend,
BigDecimal: FromSql<ST, DB>,
{
fn from_sql(bytes: Option<&DB::RawValue>) -> deserialize::Result<Self> {
BigDecimal::from_sql(bytes).map(|s| RoundedBigDecimal(s.with_prec(7)))
}
}
aha I added some derived traits and associated RoundedBigDecimal
with the Numeric
sql type:
#[derive(Debug, Clone, AsExpression, FromSqlRow)]
#[sql_type = "Numeric"]
pub struct RoundedBigDecimal(BigDecimal);
https://stackoverflow.com/questions/49092437/how-do-i-implement-queryable-and-insertable-for-custom-field-types-in-diesel <- very useful post +1