drizzle-team / drizzle-team/drizzle-orm
[BUG]: pg array type creation throws error when the type is a composite type
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### What version of `drizzle-orm` are you using?
0.31
### What version of `drizzle-kit` are you using?
_No response_
### Describe the Bug
The current implementation of creating an array in postgres creates a malformed array literal error when used with composite types. [makePgArray](https://github.com/drizzle-team/drizzle-orm/blob/e922211fdbbd1218ac78440d8ded281a7f34b347/drizzle-orm/src/pg-core/utils/array.ts#L81) creates a string literal representation of the array, which doesn't work with composite types and (I assume) other more complicated types. Using an [array constructor](https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS) is more reliable.
The composite type I'm trying to use:
```sql
CREATE TYPE "co2e_emission_factor_type" AS (
"id" varchar,
"value" numeric
)
```
```ts
const co2eEmissionFactorPgType = customType<{
data: {
id: string;
value: number;
};
driverData: string;
}>({
dataType: () => "co2e_emission_factor_type",
toDriver(data): string {
return `ROW('${data.id}', ${data.value})::co2e_emission_factor_type`;
},
fromDriver(data: string) {
const match = /\((?.+),\s?(?.+)\)/.exec(data);
if (!match) {
throw new Error("Invalid co2e_emission_factor_type format");
}
return {
id: match.groups?.id ?? "",
value: Number(match.groups?.value),
};
},
});
const ghgTable = pgTable("ghg", {
co2Emissions: co2eEmissionFactorPgType("co2_emissions").array().notNull()
});
```
### Expected behavior
I expect it to not fail.
### Environment & setup
_No response_
Contributor guide
Research direction
Start in drizzle-orm/src/pg-core/utils/array.ts at makePgArray, then reproduce the issue with the composite co2e_emission_factor_type and the .array() column example from the report. Done means PostgreSQL array creation no longer produces a malformed array literal error for composite types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, typescript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100