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
6
votes
0 answers

Why am I getting "implementation of Deref is not general enough"?

Here's my minimum reproducible example: use axum::{extract::State, Json, Router}; use diesel::pg::Pg; use diesel_async::{ pooled_connection::bb8::Pool, AsyncConnection, AsyncPgConnection, RunQueryDsl, }; fn main() { // body omitted for…
laptou
  • 6,389
  • 2
  • 28
  • 59
6
votes
1 answer

Rust Diesel, error loading results of a sql_query that select columns from multiple tables

I tried to execute an sql_query with Diesel that doesn't match a single table and I got the following error: error[E0277]: the trait bound `Untyped: load_dsl::private::CompatibleType` is not satisfied -->…
Yther
  • 121
  • 6
6
votes
2 answers

Rust diesel orm queries

I am new to rust and diesel orm. I am trying to execute below things on my query: count select order limit but I am getting error. I am using postgres database. I have added the exact error above the queries in comment. Here are my…
Glenn singh
  • 233
  • 1
  • 6
  • 18
6
votes
1 answer

How do I get autocompletion for Diesel's schema module?

I figured out how to make Diesel work with my project, but when I try to use a functionality from the schema module, I get no code completion suggestions from VS Code with the RLS extension installed. I also tried to get suggestions with the…
carlOS
  • 61
  • 2
6
votes
2 answers

How to use pooling of database inside function using actix?

I created a pool for MySQL database like this: let database_url = env::var("DATABASE_URL").expect("set DATABASE_URL"); let manager = ConnectionManager::::new(database_url); let pool = r2d2::Pool::builder() .build(manager) …
Cliff Anger
  • 195
  • 2
  • 11
6
votes
1 answer

How do I specify a Diesel DATABASE_URL when my password contains an at-sign (@)?

I am getting started with Diesel by following their official documentation. I also have PostgreSQL installed. My database username is postgres and the password is 1schoollife@. I started with $ echo…
Muhammad Naufil
  • 2,420
  • 2
  • 17
  • 48
6
votes
1 answer

Generic function using Diesel causes overflow

I have an API written in Rust and it's goal is to expose ~15 tables in a database. I've written several very similar functions to expose each table so I thought I'd take a crack at polymorphism to simplify the code. I've reduced all the code to a…
Luuk Wester
  • 189
  • 2
  • 10
6
votes
1 answer

Querying a Diesel table with dynamic parameters

I was starting to look into using Diesel for querying a database. I have a table that looks something like the struct below (this is just a toy project to help me understand how Diesel works). #[derive(Queryable,…
Blake Pettersson
  • 8,927
  • 3
  • 27
  • 36
5
votes
1 answer

Trouble & Confusion with Diesel and Rust Traits

I'm trying to use the Diesel crate (version 2.0.2; rustc 1.63.0) for an application and have some code that goes like this: src/models.rs use uuid::Uuid; use diesel::prelude::*; use crate::schema::entities::dsl::entities; type DB =…
ThirteenthWolf
  • 336
  • 3
  • 9
5
votes
1 answer

rust: Correct use of Decimal type in diesel

I'm learning to use diesel orm library, my database uses DECIMAL(8,2) type, but when I use Decimal in my model, I get an error I am using Decimal provided by rust_decimal diesel = { version="1.4.8", features = ["mysql", "r2d2", "chrono", "numeric"]…
januw a
  • 2,056
  • 5
  • 18
  • 39
5
votes
4 answers

How to install diesel_cli with "cannot open input file libpq.lib" error?

I'm trying to install diesel_cli on my machine and have run into errors: PS C:\> cargo install diesel_cli --no-default-features --features postgres error: linking with `link.exe` failed: exit code: 1181 | = note: "C:\\Program Files…
DougApplegate
  • 61
  • 1
  • 3
5
votes
1 answer

Following "getting started" diesel tutorial, but with sqlite, causes

I'm trying to follow: https://diesel.rs/guides/getting-started but I'm using: echo DATABASE_URL=/tmp/diesel_demo.sqlite > .env instead of a Postgres database. I've changed all occurrences of Pg to Sqlite, and SERIAL to INT, but get the following…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
5
votes
1 answer

How do I select a subset of columns with diesel-rs?

I am struggling for several hours now to query a subset of the available columns of a table as well as including a calculation in it. I know that it is not the best way to execute the calculation in the select query, but for now, I am just working…
Tim
  • 65
  • 2
  • 6
5
votes
3 answers

The trait `diesel::Connection` is not implemented for `DbConnection`

I'm trying to add a postgres database to a rocket app using diesel. My main.rs file looks like this, but gives the error "the trait diesel::Connection is not implemented for DbConnection" at .get_result(connection) #[macro_use] extern crate…
Michael Evans
  • 971
  • 1
  • 13
  • 30
5
votes
1 answer

How do I make and parse a raw SQL query with Diesel?

I'm testing out Rust using the Rocket framework. For the database, I'm using Diesel to interact with my MySQL database. Following a few examples, there are a lot of things happening under the hood. I've got a MySQL Database running with a populated…
cbll
  • 6,499
  • 26
  • 74
  • 117
1 2
3
26 27