diesel-geography icon indicating copy to clipboard operation
diesel-geography copied to clipboard

Unable to use `bind` with `GeogPoint`

Open mirosval opened this issue 6 years ago • 1 comments

Thank you for creating this library!

We used it in a Hackathon against COVID-19 this weekend here, but ran into an issue.

When trying to make a sql query using sql_query and bind we kept getting the error mentioned below. We're not sure what the cause is, since the ToSql trait is implemented for GeogPoint. I also asked on diesel gitter, but did not get further with it.

Would you know what the problem is?

For now we use string interpolation, which works since the types we're feeding in are numeric, but I guess is not ideal.

let res: Result<Vec<Checkin>, _> = sql_query(
                "select * from checkins where ST_DWithin(gps, $1, 1000.0, false) ",
                )
                .bind::<Geography, _>(GeogPoint { 
                    x: req.gps[0], 
                    y: req.gps[1], 
                    srid: None 
                })
                .get_results::<Checkin>(&conn);
error[E0277]: the trait bound `diesel::query_builder::sql_query::UncheckedBind<diesel::query_builder::SqlQuery, diesel_geography::types::GeogPoint, diesel_geography::sql_types::Geography>: diesel::query_dsl::LoadQuery<_, model::Checkin>` is not satisfied
  --> src/handlers.rs:27:18
   |
27 |                 .get_results::<Checkin>(&conn);
   |                  ^^^^^^^^^^^ the trait `diesel::query_dsl::LoadQuery<_, model::Checkin>` is not implemented for `diesel::query_builder::sql_query::UncheckedBind<diesel::query_builder::SqlQuery, diesel_geography::types::GeogPoint, diesel_geography::sql_types::Geography>`
   |
   = help: the following implementations were found:
             <diesel::query_builder::sql_query::UncheckedBind<Query, Value, ST> as diesel::query_dsl::LoadQuery<Conn, T>>

mirosval avatar Mar 23 '20 10:03 mirosval

@mirosval Hm, not sure, I just bind the longitude and latitude like this:

sql_query("
    ...
    ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography`
    ...
")
	.bind::<Float8, _>(lon)
	.bind::<Float8, _>(lat)

(Note: longitude is the first arg.)

AFAIK, 4326 is the most commonly used SRID that works for the whole globe.


Btw, having a GeogPoint as part of a model struct works when fetching rows.

Boscop avatar Mar 23 '20 13:03 Boscop