Closed treyhakanson closed 4 years ago
I don't see any use of uuid
in that example, so I doubt that is the problem (yet). You are correct that the uuid
versions do not match, and if/when you do start using Uuid
you will need to convert between the two versions via strings or just strings entirely in one place or another.
I believe the error occurs because you tried to convert or compare a rust String
to or from a postgres UUID
either in the WHERE clause or in the return type you requested. String
doesn't look compatible with UUID
according to the lists for ToSql
or FromSql
Awesome, you were correct about that; thanks for the help! Feels a little gnarly to have to do something like this every time a uuid is used thought:
use rocket_contrib::uuid::Uuid as RocketUuid;
use uuid::Uuid;
// ...
#[get("/foo/<id>")]
fn foo(conn: DBConn, id: RocketUuid) -> Option<String> {
let pg_uuid = Uuid::parse_str(&id.to_string()).unwrap();
match conn.query(
"
SELECT id
FROM foo
WHERE id = $1;
",
&[&pg_uuid],
) {
Ok(result) => Some(result.get(0).get::<&str, Uuid>(&"id").to_string()),
Err(e) => {
error!(target: "foo", "Error fetching foo: {}", e.to_string());
None
}
}
}
Is there a better way? Luckily the rc and alpha versions of rust-postgres
appear to be using uuid
0.7 so this hopefully won't be a problem for much longer
Converting through Bytes
/UuidBytes
(both type aliases for [u8; 16]
) instead of String
should be more efficient. You could also implement traits to provide convenient shorthand like .to_uuid5()
or .to_uuid7()
, but the version conflict is unfortunately pretty unavoidable for now.
Great, your trait idea should be enough for me to get by for now; thanks again for the help, I really appreciate it!
Rocket version: 0.4.2 OS: macOS Mojave 10.14.5
When using rocket with the
postgres
, anduuid
features, I get the following error on trying to pull a uuid out of the DB:Here is a link to a minimal example; run the SQL commands to setup the database, and create an entry, and then make the following request:
The request will return a 404, and you'll see the error mentioned above in the logs.
I'm fairly new to Rust, but my suspicion is that there is that there's a version mismatch between these libraries. The
uuid
feature ofrust-postgres
appears to only work withuuid
version 0.5, butrocket_contrib
is using0.7.4
. Also note that I tried falling back to theuuid-ossp
extension since I was originally usingpgcrypto
, but that did not help.