New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UUID usage example #326

Closed
mann-ed opened this Issue May 12, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@mann-ed

mann-ed commented May 12, 2016

I used the diesel_demo app. I added needed dependencies and a uuid column to posts. Just for simplicity i only ran the show_posts.rs. I can't seem to figure out how to use a uuid column. Error i get is

 Running `rustc bin/show_posts.rs --crate-name show_posts --crate-type bin -g --cfg feature=\"diesel_codegen\" --cfg feature=\"diesel\" --cfg feature=\"default\" --cfg feature=\"nightly\" --cfg feature=\"dotenv_macros\" --out-dir /home
/emann/sandbox/rust/diesel_demo/target/debug --emit=dep-info,link -L dependency=/home/emann/sandbox/rust/diesel_demo/target/debug -L dependency=/home/emann/sandbox/rust/diesel_demo/target/debug/deps --extern diesel_codegen=/home/emann/sand
box/rust/diesel_demo/target/debug/deps/libdiesel_codegen-ab384d78b3e11a0e.rlib --extern diesel_codegen=/home/emann/sandbox/rust/diesel_demo/target/debug/deps/libdiesel_codegen-ab384d78b3e11a0e.so --extern dotenv=/home/emann/sandbox/rust/di
esel_demo/target/debug/deps/libdotenv-695bff8444756e3d.rlib --extern uuid=/home/emann/sandbox/rust/diesel_demo/target/debug/deps/libuuid-5b93bff81ff3c817.rlib --extern diesel=/home/emann/sandbox/rust/diesel_demo/target/debug/deps/libdiesel
-314df7bf9433c77b.rlib --extern dotenv_macros=/home/emann/sandbox/rust/diesel_demo/target/debug/deps/libdotenv_macros-b005ef1b47b27d5c.so --extern diesel_demo=/home/emann/sandbox/rust/diesel_demo/target/debug/libdiesel_demo.rlib`
bin/show_posts.rs:15:10: 15:14 error: the trait bound `uuid::Uuid: diesel::types::FromSqlRow<diesel::types::Nullable<diesel::types::Uuid>, _>` is not satisfied [E0277]
bin/show_posts.rs:15         .load::<Post>(&connection)
                              ^~~~
bin/show_posts.rs:15:10: 15:14 help: run `rustc --explain E0277` to see a detailed explanation
bin/show_posts.rs:15:10: 15:14 help: the following implementations were found:
bin/show_posts.rs:15:10: 15:14 help:   <uuid::Uuid as diesel::types::FromSqlRow<diesel::types::Uuid, DB>>
bin/show_posts.rs:15:10: 15:14 note: required because of the requirements on the impl of `diesel::types::FromSqlRow<(diesel::types::Integer, diesel::types::Text, diesel::types::Text, diesel::types::Bool, diesel::types::Nullable<diesel::types::Uuid>), _>` for `(i32, std::string::String, std::string::String, bool, uuid::Uuid)`
bin/show_posts.rs:15:10: 15:14 note: required because of the requirements on the impl of `diesel::Queryable<(diesel::types::Integer, diesel::types::Text, diesel::types::Text, diesel::types::Bool, diesel::types::Nullable<diesel::types::Uuid>), _>` for `diesel_demo::models::Post`
error: aborting due to previous error
error: Could not compile `diesel_demo`.

This is my second day on learning Rust so i don't have a lot of experience. Is this just a newbie error?
models.rs

use uuid::Uuid;

#[derive(Queryable)]
pub struct Post {
    pub id: i32,
    pub title: String,
    pub body: String,
    pub published: bool,
    pub post_id: Uuid,
}

Cargo.toml

[package]
name = "diesel_demo"
version = "0.1.0"
authors = ["Sean Griffin <sean@seantheprogrammer.com>"]
build = "build.rs"

[build-dependencies]
syntex = { version = "0.31.0", optional = true }
diesel_codegen = {git = "https://github.com/diesel-rs/diesel.git" , default-features = false, features = ["nightly", "postgres"] }
dotenv_codegen = { version = "0.8.1",  optional = true }

[dependencies]
diesel = { git = "https://github.com/diesel-rs/diesel.git",  default-features=true, features = ["uuid"] }
diesel_codegen = {git = "https://github.com/diesel-rs/diesel.git" , default-features = false, features = ["nightly", "postgres"] }
dotenv = "0.8.0"
dotenv_macros = { version = "0.8.0", optional = true }
uuid = { version = "^0.2.0" }

[features]
default = ["nightly"]
with-syntex = ["syntex", "diesel_codegen/with-syntex", "dotenv_codegen"]
nightly = ["diesel/unstable", "diesel_codegen/nightly", "dotenv_macros"]

lib.rs

#![cfg_attr(feature = "nightly", feature(custom_derive, custom_attribute, plugin))]
#![cfg_attr(feature = "nightly", plugin(diesel_codegen, dotenv_macros))]

#[macro_use]
extern crate diesel;
extern crate dotenv;
extern crate uuid;

#[cfg(feature = "nightly")]
include!("lib.in.rs");

#[cfg(feature = "with-syntex")]
include!(concat!(env!("OUT_DIR"), "/lib.rs"));

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))
}
extern crate diesel_demo;
extern crate diesel;

use self::diesel_demo::*;
use self::diesel_demo::models::*;
use self::diesel::prelude::*;

fn main() {
    use diesel_demo::schema::posts::dsl::*;

    let connection = establish_connection();
    let results = posts.filter(published.eq(true))
        .limit(5)
        .load::<Post>(&connection)
        .expect("Error loading posts");

    println!("Displaying {} posts", results.len());
    for post in results {
        println!("{}", post.title);
        println!("----------\n");
        println!("{}", post.body);
    }
}

Sorry if this should not be posted this way. If so i'm sure i will get educated on the correct way.
Thanks

@sgrif

This comment has been minimized.

Member

sgrif commented May 12, 2016

The problem is that you have a nullable field but you are attempting to load it into a type which does not handle it. Rust does not have a concept of null, so you need to have the type reflect this. You either need to change post_id to have the type Option<Uuid>, or you need to change your schema so that the column is NOT NULL

@sgrif sgrif closed this May 12, 2016

@Eijebong

This comment has been minimized.

Member

Eijebong commented Jul 14, 2017

Are you sure you're having the same problem ? Can you paste the error you're seeing too ?

@vityafx

This comment has been minimized.

vityafx commented Jul 14, 2017

@Eijebong No, it's okay. I have just realized that the problem is in another table where I had simple

user_id uuid references users(id)

So it was nullable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment