Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"] }

migration/up.sql:

CREATE TABLE books (
  id INTEGER NOT NULL PRIMARY KEY,
  size INTEGER NOT NULL
);

schema.rs:

table! {
    books (id) {
        id -> Integer,
        size -> Integer,
    }
}

sources:

use crate::schema::books;

#[derive(Insertable, Queryable)]
#[table_name="books"]
pub struct BookRecord {
    pub id: Id,
    pub size: i64,
}

This gives the following error:

error[E0277]: the trait bound `i64: diesel::Expression` is not satisfied
 --> src/lib/models/book.rs:4:10
  |
4 | #[derive(Insertable, Queryable)]
  |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `i64`
  |
  = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Integer>` for `i64`
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

How do I resolve this error?

like image 333
seb_odessa Avatar asked Sep 11 '20 14:09

seb_odessa


1 Answers

i64 corresponds to BigInt and i32 corresponds to Integer. Either change your schema to use BigInt, or change your numbers to i32.

like image 125
Coder-256 Avatar answered Oct 29 '22 00:10

Coder-256