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
5
votes
1 answer

How do I perform a delete with sub-query in Diesel against a Postgres database?

I have the following schema in a Postgres database: Table A { ID Name } Table B { ID FOREIGN KEY (A.ID) } I'm trying to write the following query in Diesel: DELETE FROM B WHERE B.ID in (SELECT ID from A WHERE A.Name = $VAR) $VAR is a…
Dash83
  • 1,357
  • 2
  • 17
  • 31
5
votes
1 answer

Rust compiling error: process didn't exit successfully

Recently, I have not been able to do cargo run in Rust. The error I get is error: process didn't exit successfully: target\debug\backend.exe (exit code: 0xc0000138, STATUS_ORDINAL_NOT_FOUND) It happens after the compiler seems to finish…
mso4491
  • 171
  • 3
  • 12
5
votes
1 answer

How to fetch associations with Diesel?

I'm trying to load an association from a database using Diesel. I have the following models defined: #[derive(Identifiable, Queryable, PartialEq, Debug)] #[table_name = "contacts"] pub struct Contact { pub id: i32, pub firstname: String, …
BitMender
  • 51
  • 1
  • 3
5
votes
1 answer

"expected struct String, found struct schema::my_table::columns::my_column" when trying to insert value with Diesel

I am trying to execute an insert multiple columns using Diesel with PostgreSQL. This is the insert function to add a new Project - pub fn insert(project: NewProject, program_id: i32, conn: &PgConnection) -> bool { use schema::projects::dsl::*; …
5
votes
2 answers

Why do I get a trait not implemented for an optional field in Diesel struct

I have this struct: #[table_name = "clients"] #[derive(Serialize, Deserialize, Queryable, Insertable, Identifiable, Associations)] pub struct Client { pub id: Option, pub name: String, pub rank: Option, } and the following…
Lev
  • 1,698
  • 3
  • 18
  • 26
5
votes
1 answer

Understanding trait bound error in Diesel

I want to write a function that will insert a type into a database where the database connection parameter is generic, so that it can work on multiple backends. I came up with the following function to insert an object using a generic…
Tim
  • 4,560
  • 2
  • 40
  • 64
4
votes
0 answers

Obscure trait bound error when trying to insert a value on a table (Diesel)

I'm trying to make Diesel work, and for testing purposes, I made a "Book" table and entity, ran the migration, and then actually tried to create the "createBook" function. For a SQLite server, it looks like this: use diesel::prelude::*; use…
Zerok
  • 1,323
  • 1
  • 24
  • 56
4
votes
1 answer

How can I resolve "use of undeclared crate or module" error when using #[diesel(table_name = ...)]?

I have a Rust project using Rocket and Diesel 1.4. I am using MariaDB as my database. My model and schema is generated this way: diesel print-schema --database-url mysql://root:password@127.0.0.1:3306/mydb > src/schema.rs diesel_ext -I…
Michael
  • 892
  • 2
  • 10
  • 28
4
votes
1 answer

Rust Diesel sql_types::Binary to Vec

I am trying to read and write a byte array from from a postgres database using Diesel. the schema is like so: table! { data_table (id) { id -> Int4, data_name -> Nullable, data_bytes -> Nullable, } } The…
Simon
  • 151
  • 1
  • 7
4
votes
1 answer

How to query a JSON field with Diesel?

I have this models: use diesel::sql_types::Json; #[derive(Queryable)] pub struct GMapsLocation { pub id: i32, pub place_id: String, pub data: Json, } and am trying to query that column like this: let results = gmaps_locations …
Alper
  • 3,424
  • 4
  • 39
  • 45
4
votes
1 answer

"the trait `SupportsReturningClause` is not implemented for `Sqlite`"

I'm trying to get the diesel crate to work with SQLite but going off the getting started guide, it does not seem to work for sqlite. The code which does work with postgres but not sqlite diesel::insert_into(schema::subscriptions::table) …
Qwertie
  • 5,784
  • 12
  • 45
  • 89
4
votes
1 answer

Cannot implement trait with generic param in rust diesel

I defined a trait which has name exists_by_id_and_password. I don't want to implement it with concrete database backend, so some generic parameters were added to DB struct. but compiler reported an error: type mismatch resolving `
wangjun
  • 577
  • 1
  • 6
  • 14
4
votes
1 answer

How to do a IN query using diesel?

I want to do an IN query with diesel using PostgreSQL 13 to select songs collection from songs table in some id collections. The SQL executed by the database may look like this: select * from songs where id in(1,2,3) what I tried to do using rust…
Dolphin
  • 29,069
  • 61
  • 260
  • 539
4
votes
2 answers

Imlementing connection pooling in a rust/diesel app with r2d2

I am trying to implement connection pooling in a rust/diesel/rocket application. I am not sure how to ensure the content of the establish_pooled_connection() method is invoked only once, so as to prepare the connection pool. Here is my code. from…
balteo
  • 23,602
  • 63
  • 219
  • 412
4
votes
1 answer

Rust, Windows 10: Diesel doesn't work, diesel setup provides no output

I am new to Rust, and I wanted to try Diesel with Postgres. I have followed the steps described here: https://diesel.rs/guides/getting-started But unfortunately, when it comes do diesel setup, nothing happens. I receive no output, no error messages,…
csuvi98
  • 61
  • 3