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

Weird error with the f32 type, is real on postgre but my model doesn't work #679

Closed
freinn opened this Issue Feb 11, 2017 · 5 comments

Comments

Projects
None yet
2 participants
@freinn

freinn commented Feb 11, 2017

Hi! I have this working struct for a model:

#[derive(Queryable)]
pub struct EntradaBlog {
    pub id_entrada_blog: i32,
    pub titulo: String,
    pub fecha_publicacion: NaiveDateTime,
    pub contenido: String,
    pub tiempo_de_lectura: i16, // SmallInt
}

It runs fine and I can query my postgresql table, but I'm having problems with the last field of this struct:

#[derive(Queryable)]
pub struct Cuadro {
    pub id_cuadro: i32,
    pub titulo: String,
    pub autor: String,
    pub descripcion: String,
    pub fecha_realizacion: Option<NaiveDateTime>,
    pub id_tecnica: i32,
    pub ancho: i16,
    pub alto: i16,
    pub precio: f32,
}

The field precio is a real on postgresql:

id_cuadro         | integer                     | not null default nextval('cuadros_id_cuadro_seq'::regclass)  | plain    |              | 
 titulo            | character varying(200)      | not null                                                     | extended |              | 
 autor             | character varying(200)      | not null                                                     | extended |              | 
 descripcion       | text                        | not null                                                     | extended |              | 
 fecha_realizacion | timestamp without time zone |                                                              | plain    |              | 
 id_tecnica        | integer                     | not null default nextval('cuadros_id_tecnica_seq'::regclass) | plain    |              | 
 ancho             | smallint                    | not null                                                     | plain    |              | 
 alto              | smallint                    | not null                                                     | plain    |              | 
 precio            | real                        | not null                                                     | plain    |              | 
Indexes:
    "cuadros_pkey" PRIMARY KEY, btree (id_cuadro)
Foreign-key constraints:
    "cuadros_id_tecnica_fkey" FOREIGN KEY (id_tecnica) REFERENCES tecnicas(id_tecnica)

And when I try to do a simple query (equivalent to select * from cuadros) I get a compiler error:

error[E0277]: the trait bound `Cuadro: diesel::Queryable<(diesel::types::Integer, diesel::types::Text, diesel::types::Text, diesel::types::Text, diesel::types::Nullable<diesel::types::Timestamp>, diesel::types::Integer, diesel::types::SmallInt, diesel::types::SmallInt, diesel::types::Float), _>` is not satisfied
   --> src/main.rs:111:10
    |
111 |         .load::<Cuadro>(&connection)
    |          ^^^^ the trait `diesel::Queryable<(diesel::types::Integer, diesel::types::Text, diesel::types::Text, diesel::types::Text, diesel::types::Nullable<diesel::types::Timestamp>, diesel::types::Integer, diesel::types::SmallInt, diesel::types::SmallInt, diesel::types::Float), _>` is not implemented for `Cuadro`

What I'm doing wrong?

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 11, 2017

You're certain that column is the issue? Nothing looks out of place to me. The only thing that jumps out to me that could be the issue is a mismatch between chrono versions. Are you using Chrono 0.2.x and Diesel 0.10.x?

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 11, 2017

(Also this isn't related to your bug, but, you probably shouldn't use floats for currency. precio_en_centavos as an integer would be the standard way to do it 😉)

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 11, 2017

I attempted to reproduce your issue and was unable to do so. The code that I used to try to reproduce is at https://gist.github.com/sgrif/6a1a7af32274b19f43cad5c83131e811. This code compiled successfully. Can you please provide additional information that I can use to reproduce, preferably in the same form as that gist?

@freinn

This comment has been minimized.

freinn commented Feb 11, 2017

Thanks for the caring in your answer, and your consideration. I've uploaded my private git repo to github so you can test it easily, here. My code organization is poor. I have to split it into modules but I prefer to have all working before.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 11, 2017

You have multiple structs called Cuadro. The failure here is because Cuadro is referring to the struct here, which does not implement Queryable, not the one here which does.

@sgrif sgrif closed this Feb 11, 2017

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