sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

sqlx not converting to i64 from bigdecimal

Open SamyakGangwal opened this issue 3 years ago • 7 comments

I have a query that fetches the size of a repository in bytes. Let's take an example: the size of that file is 199624061 bytes. When I tried to fetch the size I got this 844433520066560. This is my query:

pub async fn get_repositories(
	connection: &mut <Database as sqlx::Database>::Connection,
	id: &Uuid,
) -> Result<Vec<(DockerRepository, u64)>, sqlx::Error> {
	let rows = query!(
		r#"
		SELECT
			id as "id: Uuid",
			size as "size!: i64"
		FROM
			repository
		WHERE
			id = $1;
		"#,
		id as _
	)
	.fetch_all(&mut *connection)
	.await?
	.into_iter()
	.map(|row| {
		(
			Repository {
				id: row.id,
				size,
			}
		)
	})
	.collect();

	Ok(rows)
}

I solved this by removing the i64 from the query and converted the bigdecimal to u64 using an external crate. But I should ideally be able to convert into i64 thorugh sqlx right?

SamyakGangwal avatar Feb 04 '22 19:02 SamyakGangwal

What database are you using (Postgres/MySQL/MSSQL/SQLite)? I'm guessing SQLite because you're using $1 as a bind parameter but Postgres doesn't support u64.

We actually removed support for u64 from the SQLite driver because we can't support the full range of values: https://github.com/launchbadge/sqlx/commit/2fd26b5504433fbe6873c807521e3d48f7dbc6bb

SQLite might accept UNSIGNED BIG INT for the type of a column but it'll still store it as a signed 64-bit integer.

Actually... how are you even doing this then? We removed that impl between 0.5.0 and 0.5.1 then yanked 0.5.0.

abonander avatar Feb 04 '22 20:02 abonander

What database are you using (Postgres/MySQL/MSSQL/SQLite)? I'm guessing SQLite because you're using $1 as a bind parameter but Postgres doesn't support u64.

We actually removed support for u64 from the SQLite driver because we can't support the full range of values: https://github.com/launchbadge/sqlx/commit/2fd26b5504433fbe6873c807521e3d48f7dbc6bb

SQLite might accept UNSIGNED BIG INT for the type of a column but it'll still store it as a signed 64-bit integer.

Actually... how are you even doing this then? We removed that impl between 0.5.0 and 0.5.1 then yanked 0.5.0.

Sorry, I meant i64 and not u64. I am using postgres database for this query

SamyakGangwal avatar Feb 05 '22 05:02 SamyakGangwal

And the repository.size column is NUMERIC?

abonander avatar Feb 08 '22 23:02 abonander

it is bigint

SamyakGangwal avatar Feb 09 '22 04:02 SamyakGangwal

But you mentioned "bigdecimal" a couple of times. Was that a mistake?

abonander avatar Feb 09 '22 04:02 abonander

in the create table query i make it as bigint. However in that select query when I don't cast it to i64 rust shows me that the data type of size is bigdecimal this is the create table query

CREATE TABLE repository(
			id UUID NOT NULL,
                        size BIGINT NOT NULL
		);

SamyakGangwal avatar Feb 09 '22 04:02 SamyakGangwal

Is this fixed by #3184 ?

fosskers avatar Jul 01 '24 02:07 fosskers