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
10
votes
2 answers

How to re-run Diesel migrations?

I'm using Diesel with PostgreSQL. I added my migrations, and they worked fine, outputting everything in the schema.rs file. Until I noticed that I was missing the created_at fields on some of my migrations. I edited the SQL and ran diesel migration…
Henry
  • 1,311
  • 1
  • 9
  • 26
10
votes
1 answer

Cannot infer type for `U`

I am using Rust and Diesel: fn create_asset_from_object(assets: &HashMap) { let connection: PgConnection = establish_connection(); println!("=========================================================="); …
BalaB
  • 3,687
  • 9
  • 36
  • 58
9
votes
1 answer

Creating Diesel.rs queries with a dynamic number of .and()'s

While playing with Diesel, I got stuck writing a function which takes an vector of Strings as input and does the following: Combine all Strings to a large query run the query on the Connection process the result return a Vec If I construct the…
8
votes
5 answers

cannot link libpq on Mac M1

I've been trying to run the Rust Diesel crate on my Macbook M1 and it doesn't work. The final part of the compilation gets broken by the following error: = note: ld: warning: ignoring file /usr/local/Cellar/libpq/14.1/lib/libpq.dylib, building for…
max89
  • 443
  • 5
  • 18
8
votes
1 answer

How to use i64 with Insertable using Diesel

How do I use i64/u64 with Diesel? Do I really need to implement the diesel::Expression trait for the primitive type? Here is my code. Cargo.toml: [dependencies] ... diesel = { version = "1.4.5", features = ["sqlite", "numeric"]…
seb_odessa
  • 121
  • 4
8
votes
1 answer

Performing a Right Join in Diesel

I have a function that takes two optional filtering arguments, and filters the data in a table if these arguments are provided. To do this, I created a boxed query. I would like to create a right join with this boxed query. Diesel's current…
Luuk Wester
  • 189
  • 2
  • 10
8
votes
1 answer

Retrieve datetime from mySQL database using Diesel

I can't retrieve datetime from a populated mySQL database using Rocket and Diesel. Here is my model: extern crate chrono; use diesel::prelude::*; use diesel::mysql::MysqlConnection; use schema::chrisms; use diesel::sql_types::Datetime; use…
Leonel Sá
  • 81
  • 1
  • 3
7
votes
3 answers

rust diesel linking with `cc` failed

Ok, so kind of getting nowhere here. Before I posted a problem with my Mac M1 having linker issues with Rust Diesel and got nothing. So I spun up an Ec2 instance and tried to run this crate here and got the following: error: linking with `cc`…
max89
  • 443
  • 5
  • 18
7
votes
1 answer

Rust Diesel not building with error use of undeclared crate or module

I am trying to create my first Rust app with database support, I am using Diesel with SQLite, whenever I build my application I get the following error message: failed to resolve: use of undeclared crate or module `categorys` -->…
user12510970
7
votes
2 answers

How to get the id of a newly-created value with Diesel and SQLite?

Diesel's SqliteBackend does not implement the SupportsReturningClause trait, so the get_result method cannot be used to retrieve a newly created value. Is there another way to find out the id of the inserted row? Python has a solution for this. The…
Maxim Gritsenko
  • 2,396
  • 11
  • 25
7
votes
2 answers

Rust diesel conditionally filter a query

I am trying to use diesel for a project and I would like to have a "filterable" type. The idea is that you can go to /api/foo?id=10&bar=11 and it would return a struct Foo: struct Foo { id: Option, bar: Option, name:…
nadir
  • 73
  • 1
  • 3
7
votes
0 answers

How do I write a GROUP BY or HAVING clause in Diesel?

I'm trying to convert the following SQL query into corresponding Rust Diesel code: SELECT COUNT(*) FROM BookStore WHERE BookName IN ('Lord of the Rings', 'Hobbit') GROUP BY StoreId HAVING COUNT(DISTINCT BookName) = 2 I was able to translate it thus…
user1842633
  • 305
  • 1
  • 4
  • 15
7
votes
1 answer

Cannot use UUID as a primary key: Uuid: diesel::Expression is not satisfied

I want to have a UUID field as the primary field in a Postgres table, but I get the following error: error[E0277]: the trait bound `uuid::Uuid: diesel::Expression` is not satisfied --> database/src/models.rs:3:35 | 3 | #[derive(Debug, Clone,…
Ayush Singh
  • 1,409
  • 3
  • 19
  • 31
7
votes
1 answer

Does Diesel support the Postgres `point` type?

I'd like to use the point Postgres type. I don't see this type listed here in the types list for Diesel. What is the correct way to write a model that includes a point column?
lovelikelando
  • 7,593
  • 6
  • 32
  • 50
7
votes
0 answers

Cannot get Diesel to infer a nullable Timestamp column in a SQLite table

I'm trying to use Diesel's inference capability to work with a SQLite table that contains a nullable timestamp column. A complete example is available in an older revision of this GitHub project. The books table is defined as follows: create table…
Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
1
2
3
26 27