如何使用 Diesel 将 i64 与 Insertable 结合使用 [英] How to use i64 with Insertable using Diesel
本文介绍了如何使用 Diesel 将 i64 与 Insertable 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Diesel 中使用 i64
/u64
?
How do I use i64
/u64
with Diesel?
我真的需要为原始类型实现 diesel::Expression
trait 吗?
Do I really need to implement the diesel::Expression
trait for the primitive type?
这是我的代码.
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)
我该如何解决这个错误?
How do I resolve this error?
推荐答案
i64
对应于 BigInt
和 i32
对应于Integer
.将架构更改为使用 BigInt
,或将数字更改为 i32
.
i64
corresponds to BigInt
and i32
corresponds to Integer
. Either change your schema to use BigInt
, or change your numbers to i32
.
这篇关于如何使用 Diesel 将 i64 与 Insertable 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文