Skip to content
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

[BUG]: pg array type creation throws error when the type is a composite type #2430

Open
anthonyma94 opened this issue Jun 3, 2024 · 1 comment
Labels
db/postgres enhancement New feature or request

Comments

@anthonyma94
Copy link

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 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 is more reliable.

The composite type I'm trying to use:

CREATE TYPE "co2e_emission_factor_type" AS (
  "id" varchar,
  "value" numeric
)
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 = /\((?<id>.+),\s?(?<value>.+)\)/.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

@anthonyma94 anthonyma94 added the bug Something isn't working label Jun 3, 2024
@L-Mario564 L-Mario564 added enhancement New feature or request db/postgres and removed bug Something isn't working labels Oct 22, 2024
@L-Mario564
Copy link
Collaborator

Drizzle currently doesn't support composite types, so I've marked this as an enhancement instead of a bug as the ORM would need to be provide support for composite types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db/postgres enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants