-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Internal error: use of unresolved type declaration #1477
Comments
I managed to reproduce the issue in the test suite. I am still working on the fix. #[sqlx_macros::test]
async fn it_supports_composite_types_in_arrays() -> anyhow::Result<()> {
// Only supported in Postgres 11+
let mut conn = new::<Postgres>().await?;
if matches!(conn.server_version_num(), Some(version) if version < 110000) {
return Ok(());
}
conn.execute(
r#"
DROP TABLE IF EXISTS pets;
DROP DOMAIN IF EXISTS pet_name_and_race_array;
DROP TYPE IF EXISTS PET_NAME_AND_RACE;
CREATE TYPE PET_NAME_AND_RACE AS (
name TEXT,
race TEXT
);
CREATE DOMAIN pet_name_and_race_array AS PET_NAME_AND_RACE[];
CREATE TABLE pets (
owner TEXT NOT NULL,
name TEXT NOT NULL,
race TEXT NOT NULL,
PRIMARY KEY (owner, name)
);
INSERT INTO pets(owner, name, race)
VALUES
('Alice', 'Foo', 'cat');
INSERT INTO pets(owner, name, race)
VALUES
('Alice', 'Bar', 'dog');
"#,
)
.await?;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct PetNameAndRace {
name: String,
race: String,
}
impl sqlx::Type<Postgres> for PetNameAndRace {
fn type_info() -> sqlx::postgres::PgTypeInfo {
sqlx::postgres::PgTypeInfo::with_name("pet_name_and_race")
}
}
impl<'r> sqlx::Decode<'r, Postgres> for PetNameAndRace {
fn decode(
value: sqlx::postgres::PgValueRef<'r>,
) -> Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
let mut decoder = sqlx::postgres::types::PgRecordDecoder::new(value)?;
let name = decoder.try_decode::<String>()?;
let race = decoder.try_decode::<String>()?;
Ok(Self {name, race})
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct PetNameAndRaceArray(Vec<PetNameAndRace>);
impl sqlx::Type<Postgres> for PetNameAndRaceArray {
fn type_info() -> sqlx::postgres::PgTypeInfo {
// Array type name is the name of the element type prefixed with `_`
sqlx::postgres::PgTypeInfo::with_name("_pet_name_and_race")
}
}
impl<'r> sqlx::Decode<'r, Postgres> for PetNameAndRaceArray {
fn decode(
value: sqlx::postgres::PgValueRef<'r>,
) -> Result<Self, Box<dyn std::error::Error + 'static + Send + Sync>> {
Ok(Self(Vec::<PetNameAndRace>::decode(value)?))
}
}
let mut conn = new::<Postgres>().await?;
let row = sqlx::query("select owner, array_agg(row(name, race)::pet_name_and_race) as pets from pets group by owner")
.fetch_one(&mut conn)
.await?;
let pets: PetNameAndRaceArray = row.get("pets");
assert_eq!(pets.0.len(), 2);
Ok(())
} |
This commit fixes the array decoder to support custom types. The core of the issue was that the array decoder did not use the type info retrieved from the database. It means that it only supported native types. This commit fixes the issue by using the element type info fetched from the database. A new internal helper method is added to the `PgType` struct: it returns the type info for the inner array element, if available. Closes launchbadge#1477
I sent a PR ( #1483 ) to fix the issue. The root cause was that the array decoder ignored the type info fetched from the DB, preventing it from supporting custom types. |
This commit fixes the array decoder to support custom types. The core of the issue was that the array decoder did not use the type info retrieved from the database. It means that it only supported native types. This commit fixes the issue by using the element type info fetched from the database. A new internal helper method is added to the `PgType` struct: it returns the type info for the inner array element, if available. Closes launchbadge#1477
@jplatte @abonander Could you comment on any of those PRs? This issue currently forces me to use my temporary fork of the lib, and I would much rather prefer to use the official version. |
I am not sure I understand your reply: both PRs are about improving support for arrays of custom types, and they don't touch the Postgres protocol. The only change my PR does is that it reuses the type info already fetched from the DB to read arrays (so it is not restricted to built-in types). |
My PR doesn't change the behavior of how things are read / written. It is simply an addition that allows users to declare (through the new |
The PR of @demurgos has solved the issue perfectly! Can it be merged anytime soon? |
I have the same issue. |
This commit fixes the array decoder to support custom types. The core of the issue was that the array decoder did not use the type info retrieved from the database. It means that it only supported native types. This commit fixes the issue by using the element type info fetched from the database. A new internal helper method is added to the `PgType` struct: it returns the type info for the inner array element, if available. Closes #1477
I am trying to use a Postgres composite type but get the following error:
When querying
pg_type
manually, I can retrieve the type (it is my composite type). It means that there is an issue somewhere in the type resolver. I will further investigate the issue and send a PR to fix it.The text was updated successfully, but these errors were encountered: