Questions tagged [rust-diesel]

This tag should be used for questions related to the diesel Rust ORM.

Diesel is an object relational mapper written in Rust. Diesel makes it easy to interact with SQL databases such as PostgreSQL and SQLite in a type-safe way.

Producing a Minimal, Reproducible Example (MRE) for Diesel

All of the general rules for a MRE apply, as do those for creating a Rust-specific MRE (see "Producing a Minimal, Reproducible Example (MRE) for Rust code").

You should include information like:

  • What database system you are using (e.g. Postgres, MySQL, SQLite, etc.)
  • Your schema definition(s)
  • Your model definition(s)

You can combine all of your code into a single file, like this:

#[macro_use]
extern crate diesel;

mod schema {
    table! {
        users (user_id) {
            user_id -> Int4,
            email -> Text,
        }
    }

    #[derive(Debug, Identifiable)]
    #[primary_key(email)]
    pub struct User {
        pub user_id: i32,
        pub email: String,
    }
}

fn main() {}
392 questions
34
votes
2 answers

Timestamp in Rust's Diesel Library with Postgres

I have been taking a look at Rust's Diesel ORM today by following along on this walk-through, and I can't get a Timestamp to work. Cargo.toml [dependencies] diesel = { version = "0.6.2", features = ["chrono"] } diesel_codegen = { version = "0.6.2",…
erewok
  • 7,555
  • 3
  • 33
  • 45
23
votes
2 answers

How do I implement Queryable and Insertable for custom field types in Diesel?

I have an SQL table that I want to work with through Diesel: CREATE TABLE records ( id BIGSERIAL PRIMARY KEY, record_type SMALLINT NOT NULL, value DECIMAL(10, 10) NOT NULL ) This table generates the following schema: table! { …
hweom
  • 437
  • 4
  • 11
19
votes
8 answers

How to fix diesel_cli link libpq.lib error with Postgres tools installed in Docker?

I'm trying (for hours now) to install the cargo crate diesel_cli for postgres. However, every time I run the recommended cargo command: cargo install diesel_cli --no-default-features --features postgres I wait a few minutes just to see the same…
CoderLee
  • 3,079
  • 3
  • 25
  • 57
19
votes
2 answers

Rust/Diesel: How to query and insert into postgres tables which have uuid

I have the following schema generated by Diesel: table! { user (id) { id -> Uuid, name -> Text } and the associated model use diesel::{ self, Queryable, Insertable, }; use diesel::prelude::*; use diesel::sql_types::Uuid; use…
Jamie Davenport
  • 351
  • 3
  • 10
18
votes
3 answers

the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`

I am trying to create a struct that I can use in diesel for insertion. Specifically I am making the struct Insertable. On compile I get this error. I have a struct that I am trying to make Insertable via the derive attribute. I have a field called…
user583824
17
votes
3 answers

PostgreSQL authentication method 10 not supported

I'm trying to follow the diesel.rs tutorial using PostgreSQL. When I get to the Diesel setup step, I get an "authentication method 10 not supported" error. How do I resolve it?
chasahodge
  • 373
  • 1
  • 4
  • 11
16
votes
1 answer

Should diesel be run using a sync actor, actix_web::web::block or futures-cpupool?

Background I am working on an actix-web application using diesel through r2d2 and am unsure of how to best make asynchronous queries. I have found three options that seem reasonable, but am unsure of which one is best. Potential Solutions Sync…
logina
  • 247
  • 1
  • 9
15
votes
1 answer

Rust Diesel: the trait bound `NaiveDateTime: Deserialize<'_>` is not satisfied

I am new to rust and diesel. And trying to create a small demo api using rocket framework. Getting error: the trait bound NaiveDateTime: Deserialize<'_> is not satisfied I googled and found some useful links like here :…
Shaksham Singh
  • 491
  • 1
  • 5
  • 19
15
votes
1 answer

What is the standard pattern to relate three tables (many-to-many relation) within Diesel?

Database - Postgres I have the following relation: users <—>> users_organizations <<—> organizations Schema: table! { organizations (id) { id -> Int4, name -> Varchar, } } table! { users (id) { id -> Int4, …
ivan_ochc
  • 392
  • 5
  • 22
15
votes
2 answers

Execute an insert or update using Diesel

I am trying to execute an insert or update using Diesel with PostgreSQL. I have tried: diesel::insert_into($table::table).values(&objects).on_conflict($table::id).do_update().set(&objects).execute(conn).unwrap(); where objects is a…
Ronny
  • 454
  • 4
  • 15
13
votes
2 answers

How to use Diesel with SQLite connections and avoid `database is locked` type of errors

In my Rust application I am using Diesel to interact with an SQLite database. I have multiple threads that may query at the same time the database, and I am using the crate r2d2 to create a pool of connections. The issue that I am seeing is that I…
gliderkite
  • 8,828
  • 6
  • 44
  • 80
13
votes
1 answer

How do I get an Option instead of an Option> from a Diesel query which only returns 1 or 0 records?

I'm querying for existing records in a table called messages; this query is then used as part of a 'find or create' function: fn find_msg_by_uuid<'a>(conn: &PgConnection, msg_uuid: &Uuid) -> Option> { use schema::messages::dsl::*; …
dch
  • 133
  • 1
  • 4
12
votes
1 answer

"use of undeclared type or module" when using Diesel's `belongs_to` attribute

I'm loosely following Diesel's getting started guide trying to set up a relational database, but getting the following error on compile: error[E0433]: failed to resolve: use of undeclared type or module `birds` --> src/models.rs:9:12 | 9 | pub…
crash springfield
  • 1,072
  • 7
  • 17
  • 33
11
votes
1 answer

mysql - the trait `diesel::Expression` is not implemented for `f64`

I'm trying to set up a model using diesel and mysql. My first model compiles without issue but when my second model, the one meant for inserting a new entry to my DB, tries to derive Insertable I get a slew of errors stating that diesel can't use…
twiclo
  • 589
  • 6
  • 10
10
votes
1 answer

how to make diesel auto generate model

I am now using this command to generate schema in rust diesel: diesel --database-url postgres://postgres:kZLxttcZSN@127.0.0.1:5432/rhythm \ migration run --config-file="${CURRENT_DIR}"/diesel-rhythm.toml and this is the toml…
Dolphin
  • 29,069
  • 61
  • 260
  • 539
1
2 3
26 27