我正在尝试在 Diesel 中使用该#[primarykey()]宏,但收到一条未知错误。根据我的发现,添加#![feature(primary_key)]应该可以解决问题,但事实并非如此。
库文件
#[macro_use]
extern crate diesel;
extern crate dotenv;
pub mod schema;
pub mod models;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
Run Code Online (Sandbox Code Playgroud)
模型.rs
#![feature(primary_key)]
extern crate diesel;
#[derive(Queryable, Debug)]
#[primary_key(user_id)]
pub struct User {
pub user_id: i32,
pub email: String,
pub password: String,
pub bio: String,
pub verified: bool,
}
Run Code Online (Sandbox Code Playgroud)
我也尝试添加#![feature(primary_key)]到顶部但lib.rs没有任何运气。
使用 Rust 1.26.0-nightly (80785a547 2018-03-30)
该primary_key属性仅在派生时适用Identifiable:
#[macro_use]
extern crate diesel;
mod schema {
table! {
users (user_id) {
user_id -> Int4,
email -> Text,
}
}
#[derive(Debug, Identifiable)]
#[primary_key(email)]
pub struct User {
pub user_id: i32,
pub email: String,
}
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
我相信您也可以更改模式定义中的主键(users (user_id))。
| 归档时间: |
|
| 查看次数: |
1071 次 |
| 最近记录: |