【问题标题】:How to use i64 with Insertable using Diesel如何使用 Diesel 将 i64 与 Insertable 一起使用
【发布时间】:2020-12-30 01:44:46
【问题描述】:

如何将i64/u64 与 Diesel 一起使用?

我真的需要为原始类型实现 diesel::Expression 特征吗?

这是我的代码。

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,
    }
}

来源:

use crate::schema::books;

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

这会产生以下错误:

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)

如何解决此错误?

【问题讨论】:

    标签: sqlite rust rust-diesel


    【解决方案1】:

    i64 corresponds to BigInti32 corresponds to Integer。将您的架构更改为使用BigInt,或将您的号码更改为i32

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 2021-12-05
      • 2021-01-02
      • 2021-12-18
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多