我有一个输入 JSON:
{"key1": "val1", "key2": 1}
我想将它存储在一个 sqlite 数据库中,以便稍后以完全相同的值响应一些 API 请求。
这是我的迁移:
CREATE TABLE my_table (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
arbitrary_json TEXT NOT NULL
);
我的Cargo.toml
:
[package]
name = "diesel_playground"
version = "0.1.0"
authors = ["User <user@example.com>"]
edition = "2018"
[dependencies]
diesel = { version = "1.4" , features = ["sqlite"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
使用以下代码:
#[macro_use]
extern crate diesel;
mod schema;
use schema::my_table;
use serde::{Deserialize, Serialize};
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use std::env;
pub fn establish_connection() -> SqliteConnection {
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
SqliteConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
#[derive(Debug, Deserialize, Serialize, Queryable, Identifiable)]
#[table_name = "my_table"]
struct Item {
id: i32,
arbitrary_json: serde_json::Value,
}
#[derive(Debug, Deserialize, Serialize, Insertable, Queryable)]
#[table_name = "my_table"]
struct NewItem {
arbitrary_json: serde_json::Value,
}
fn main() {
let my_json = serde_json::json!({
"key1": "val1",
"key2": 1
});
let new_item = NewItem {
arbitrary_json: my_json,
};
let conn = establish_connection();
diesel::insert_into(my_table::table)
.values(&new_item)
.execute(&conn)
.expect("Error adding new item");
let my_item = my_table::table
.find(1)
.first::<Item>(&conn)
.expect("Error selecting id 1");
assert!(my_item.arbitrary_json == new_item.arbitrary_json);
}
我收到以下错误:
error[E0277]: the trait bound `serde_json::value::Value: diesel::Expression` is not satisfied
--> src/main.rs:27:41
|
27 | #[derive(Debug, Deserialize, Serialize, Insertable, Queryable)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `serde_json::value::Value`
我可以创建一个带有String
JSON 表示的模型并为 API 输入类型派生From
/ Into
,但我现在不想.into()
在我的代码中到处插入。一个 DRY 解决方案将按照我在附加代码中提出的那样执行此操作。