-
Notifications
You must be signed in to change notification settings - Fork 23
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
Getting "no binary output function available for type ulid" using SQLx from Rust #27
Comments
Here's a minimal program in Rust that exhibits the error: use sqlx::postgres::PgPoolOptions;
use sqlx::Row;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let connect = "postgres://foo:bar@localhost/db";
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(connect)
.await?;
let rows = sqlx::query("SELECT * from sources")
.fetch_all(&pool)
.await?;
println!(
"rows: {:?}",
rows.into_iter()
.map(|row| row.get::<String, _>("source_id"))
.collect::<Vec<_>>()
);
Ok(())
} And here's the full error:
|
I noticed in the source code that there are implicit conversions to/from UUIDs / ULIDs, so I found this workaround: create table foo (
foo_id uuid primary key default gen_ulid(),
name text not null
); In other words, store the ULID as a UUID, retrieve it through SQLx as a UUID, and convert to/from ULID in the application. Not ideal, but it works. |
@workingjubilee Can I get some help from you to understand this issue? I store the type as binary but the Reading launchbadge/sqlx#1269, Does sqlx need another set of |
I believe PGRX simply does not adequately support this case unfortunately: we do not have a way to describe the |
I am reading all these issues... I wonder if this extension, which looks really good on paper, is prod ready? |
I use it in production. |
indeed, I've been playing with it and it's neat and handy. thank you. |
Hi @pksunkara
How are you storing created ULids (when not using the extension to generate them)? I started to use the extension and found this issue, In my case I am creating the Ulid with: let token = Ulid::new(); And trying to store it with (sqlx) let query = "INSERT INTO tokens (id, client_id) VALUES ($1, $2) RETURNING id";
let insert_token = sqlx::query(query)
.bind(token.to_string())
.bind(client_id)
.fetch_one(&mut *tx)
.await; But I get this error:
I managed to insert by casting the id, for example: let query = "INSERT INTO tokens (id, client_id) VALUES ($::ulid, $2)"; But then for retrieving the ID I get the same error from the title of this issue: let query = "INSERT INTO tokens (id, client_id) VALUES ($::ulid, $2) RETURNING id"
Manage to make it work using: let query = "INSERT INTO tokens (id, client_id) VALUES ($1::ulid, $2) RETURNING id::text";
let insert_token = sqlx::query(query)
.bind(token.to_string())
.bind(client_id)
.fetch_one(&mut *tx)
.await;
let result = match insert_token {
Ok(row) => {
let token_id: String = row.get("id");
let metadata_query =
"INSERT INTO metadata (id, ip_address, country, user_agent) VALUES ($1::ulid, $2, $3, $4)";
sqlx::query(metadata_query)
.bind(token_id)
.bind(ip_address)
.bind(country)
.bind(ua)
.execute(&mut *tx)
.await
}
Err(err) => Err(err),
}; But don't know if casting the result to test |
I'm having the same problem. To fix in the meanwhile, I updated my struct "UserData.id" to type String. And during the query applied a cast "id::text": match sqlx::query_as::<_, UserData>(
r#"
INSERT INTO users (name, email,) VALUES ($1, $2) RETURNING
id::text, name, email
"#,
) |
I've just tried this with Entity Framework/.net and I've hit the same issue. Not that helpful I know but I can get it working with this extension: https://github.com/andrielfn/pg-ulid. I'd prefer to use this extension though as it looks to be supported on Crunchy now |
I just installed pgx_ulid into a Postgres 15 database, and while inserting and viewing in a database tool (DataSpell) works fine, when I try to query from Rust using SQLx I get a "no binary output function available for type ulid" error. I thought it might be due to how I was implementing SQLx's
Encode
andDecode
traits, but I reworked those to be based on strings and I'm still getting the error. I'll paste some example code shortly.The text was updated successfully, but these errors were encountered: