diesel: [E0277]: the trait bound `i32: diesel::types::FromSqlRow, diesel::pg::Pg>` is not satisfied
Hello, when I try to use diesel to build my first orm, and error is come. My way is exactly same to your getting-start doc.
up.sql:
CREATE TABLE USERS (
uid VARCHAR PRIMARY KEY,
realname VARCHAR NOT NULL,
age INTEGER,
sex VARCHAR
);
down.sql:
DROP TABLE USERS;
.env:
DATABASE_URL=postgres://postgres:postgres@localhost/simllsan
and the command diesel migration redo has been executed successfully :
fuying@fuying-linux:~/rustprojs/simtraining$ diesel migration redo
Rolling back migration 20160514210851
Running migration 20160514210851
lib.rs:
#![feature(custom_derive, custom_attribute, plugin)]
#![plugin(serde_macros, diesel_codegen, dotenv_macros)]
#[macro_use]
extern crate diesel;
extern crate dotenv;
extern crate serde;
extern crate serde_json;
extern crate time;
extern crate rand;
extern crate libc;
extern crate mio;
#[cfg(windows)]
extern crate winapi;
#[cfg(windows)]
extern crate ws2_32;
pub mod jizu;
pub mod simctrl;
pub mod util;
pub mod zhiling;
pub mod dianzhan;
pub mod duanluqi;
pub mod fuzai;
pub mod node;
pub mod zhilu;
pub mod xitong;
pub mod schema;
pub mod user;
// pub mod evaluation;
// pub mod projects;
// pub mod station;
// pub mod training;
use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;
use diesel::result::Error;
use user::{User, NewUser};
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))
}
pub fn create_user<'a>(conn: &PgConnection,
_uid: &'a str,
_realname: &'a str,
_age: i32,
_sex: &'a str)
-> QueryResult<User> {
use schema::users;
let uid_s = String::from(_uid);
let result = users::table.find(uid_s).first(conn);
match result {
Err(NotFound) => {
let new_user = NewUser {
uid: _uid,
realname: _realname,
age : _age,
sex : _sex,
};
return diesel::insert(&new_user).into(users::table)
.get_result(conn);
}
Ok(_) => return Err(Error::DatabaseError("此用户名已存在".to_string())),
}
}
schema.rs:
infer_schema!(dotenv!("DATABASE_URL"));
user.rs:
use super::schema::users;
#[derive(PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
pub enum Sex {
Male,
Female,
Unisex,
}
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize, Queryable)]
pub struct User {
pub uid: String,
pub realname: String,
pub age: i32,
pub sex: String,
}
#[insertable_into(users)]
pub struct NewUser<'a> {
pub uid: &'a str,
pub realname: &'a str,
pub age: i32,
pub sex: &'a str,
}
impl User {
pub fn new(_id: &str, _realname: &str) -> User {
User {
uid: String::from(_id),
realname: String::from(_realname),
age: 0,
sex: "".to_string(),
}
}
}
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (4 by maintainers)
This is correct. You cannot deserialize a column of type
Nullable<Integer>into ani32. You’ll either need to use anOption<i32>on the Rust side, or change the field to beNOT NULLon the SQL side.If you ever land here looking for solution to not implemented traits compilation errors make sure that your model declaration fields follow exact order of fields in your table.
Just a note, I faced similar issue. it’s important that after you’ve made changes to the migration, run
cargo cleanFrom my limited understanding, I assume the custom code generator does not update the generated code and uses the old database definition, So we need to clean the generated code
I am sorry, for least clear explanation as I’m sleepy and just figured out the problem. I hope it helps someone.
@tmahmood thank you 😃 your solution solved my issue
@skovmand That is already mentioned in multiple locations like the getting started guide or the documentation of the
Queryablederive If you have another location in mind where you expect this information please open a PR.