I'm trying to use the #[primarykey()]
macro in Diesel but am getting an error that it is unknown. From what I have found, adding #![feature(primary_key)]
should solve the issue, but it doesn't.
lib.rs
#[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))
}
models.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,
}
I've also tried adding #![feature(primary_key)]
to the top of lib.rs
without any luck.
Using Rust 1.26.0-nightly (80785a547 2018-03-30)
The primary_key
attribute is only applicable when deriving 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() {}
I believe you could also just change your primary key in your schema definition (users (user_id)
).