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
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
schema.rs
files and use them together. You might want to add some additional joinable!
and allow_tables_to_appear_in_same_query!
calls to allow combining tables form different schemas, but otherwise it will just work as for a single schema.diesel_cli
assumes that there is a single schema. So you either need to use two different diesel.toml
files or be careful which flags to pass when so that the cli tools always use the correct schema. (That written: That's a topic where I would be open for improvements if someone wants to work on that)
sql
function to do it which work perfectly ! user_dsl::users.filter(
sql::<Bool>("account_status & ")
.bind::<BigInt, _>(AccountStatus::DailyRewardAck as i64)
.sql(" = ")
.bind::<BigInt, _>(AccountStatus::DailyRewardAck as i64),
),
diesel_infix_operator!
which allows you to extend the dsl to support additional operators: https://docs.diesel.rs/1.4.x/diesel/macro.diesel_infix_operator.html#example-usage
Hi,
I am trying to retrieve the ID of an inserted item with SQLite3.
I see that SQLite 3.5 now supports the returning
clause and apparently diesel 2.0.0-rc.0 also has implemented the change ( the changelog says: "Added support for RETURNING expressions for Sqlite via the returning_clauses_for_sqlite_3_35 feature").
Is there a possibility to get the ID of an inserted row with:
diesel = {version = "1.4.8", features = ["sqlite"]}
SQLlite3 3.31.1
Or do I need to upgrade my environment accordingly?
So far I am trying to follow this Stack Overflow answer that suggests to use the SQLite last_insert_rowid()
function. Unfortunately I have no idea when exactly to call this function. What I have tried so far is to define that function:
diesel::no_arg_sql_function!(
last_insert_rowid,
diesel::sql_types::Integer,
"last_insert_rowid()"
);
When I have an insert statement like this:
match diesel::insert_into(my_table)
.values(&my_insertable_struct)
.execute(&my_db_connection)
{
Ok(written_count) => (),
Err(e) => (),
}
Where and how would I call that function? The return type in the Ok() path is now the count of the inserted rows.
Would this turn into the last inserted row ID if I called the last_insert_rowid()
function?
Sorry for this stupid question, I am pretty clueless here.
139 | .first(&connection)
| ^^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Text>, Sqlite>` is not implemented for `*const str`
Option<String>
…
.first(…).optional()
, so I guess I’d better store it in an Option<Option<String>>
:P
I'm trying to select rows by filtering on a boolean column in a table in sqlite and facing a strange issue. My table schema is basically
table! {
table_name (id) {
id -> Integer,
name -> Text,
bool_column -> Bool,
}
}
All of my rows have the bool column to false
, which I have verified. However, the following returns me nothing
table_name.filter(bool_column.eq(false)).load::<Table>(connection)
whereas I do get all the rows if I get rid of the filter statement. Is this expected behavior? What is the correct way to do this?