Diesel: the trait `diesel::query_builder::QueryFragment<DB>` is not implemented for `u32`

Created on 26 Dec 2016  ·  4Comments  ·  Source: diesel-rs/diesel

This is probably a simple error, but there aren't really any docs about how Insertable works, and the compiler error is not helpful.

use super::schema::files;

#[derive(Debug, Queryable)]
pub struct File {
    pub id: i64,
    pub file: String,
    pub last_sync: Option<i64>,
    pub size: i64,
    pub attributes: u32,
}

#[derive(Debug, Insertable)]
#[table_name="files"]
pub struct NewFile<'a> {
    pub file: &'a str,
    pub size: i64,
    pub attributes: u32,
}

Gives:

error[E0277]: the trait bound `i64: diesel::Expression` is not satisfied
  --> src\cache\models.rs:15:1
   |
15 | #[derive(Debug, Insertable)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `i64`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert i64`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::types::Integer>` for `&'insert i64`
   = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `u32: diesel::Expression` is not satisfied
  --> src\cache\models.rs:15:1
   |
15 | #[derive(Debug, Insertable)]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `u32`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert u32`
   = note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::types::Integer>` for `&'insert u32`
   = note: this error originates in a macro outside of the current crate

error[E0277]: the trait bound `i64: diesel::Expression` is not satisfied
  --> src\cache\models.rs:15:1

... etc

This error suggests that you can't insert integers? I can't imagine that is true, though the tests don't have any integers that aren't ids.

Most helpful comment

The error message is telling you that you are using the wrong integer types. Integer is the equivalent to i32. If you want to use i64 in Rust, you should use BigInt instead. SQL does not have unsigned integers which is why you cannot use u32

All 4 comments

Oh, and I'm using the SQLite backend. That's probably important 😆

The error message is telling you that you are using the wrong integer types. Integer is the equivalent to i32. If you want to use i64 in Rust, you should use BigInt instead. SQL does not have unsigned integers which is why you cannot use u32

Thanks! :+1:

INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

But as soon as INTEGER values are read off of disk and into memory for processing, they are converted to the most general datatype (8-byte signed integer).

http://www.sqlite.org/datatype3.html#storageclasses

Seems INTEGER should map to i64 in SQLite3?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

gutsle picture gutsle  ·  4Comments

kollapsderwellenfunktion picture kollapsderwellenfunktion  ·  4Comments

qmx picture qmx  ·  3Comments

pwoolcoc picture pwoolcoc  ·  3Comments