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]: RDS Driver fails to insert arrays into PG #4038

Open
1 task done
amandaharlin opened this issue Jan 29, 2025 · 0 comments
Open
1 task done

[BUG]: RDS Driver fails to insert arrays into PG #4038

amandaharlin opened this issue Jan 29, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@amandaharlin
Copy link

amandaharlin commented Jan 29, 2025

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.31.0

What version of drizzle-kit are you using?

0.22.0

Other packages

No response

Describe the Bug

RDS Driver fails to insert array values into PostgreSQL

Issue Description

When using the AWS RDS Driver with Drizzle ORM, attempting to insert an array into PG columns fails with UUID parsing errors. This happens when trying to insert string arrays directly, empty arrays, or enum values. What did work was using sql.raw() with (string_to_array('${items.join(',')}', ',')::text[])

I haven't seen any issues on GH or discord about this, but I ran into it last night.


Example column and table definition:

export const items_table = pgTable('items', {
  id: uuid('id').defaultRandom().primaryKey()
  type: text('type', { enum: item_type.enumValues }).$type<ItemType>(),
  tags: text('tags')
    .array()
    .notNull()
    .default(sql`ARRAY[]::text[]`)
    .$type<ItemTag[]>(),
});

The generated SQL from the migration file:

ALTER TABLE "resources" ALTER COLUMN "channels" SET DATA TYPE text[];

The generated SQL above didn't work. This casting change fixed that.

ALTER TABLE "items" ALTER COLUMN "tags" SET DATA TYPE text[] USING tags::text[];

What didn't work

  1. Direct array insertion
  const items: (typeof items_table.$inferInsert)[] = MOCK_ITEMS.map((x) => {
    return {
      ...x,
      type: x. type,
      tags: ["EXAMPLE"],
    };
  });

  await pg.insert(items_table).values(items);

It gives this error:

/Users/amandaharlin/eyeframe/project/node_modules/@aws-sdk/client-rds-data/dist-cjs/index.js:912
  const exception = new DatabaseErrorException({
                    ^

DatabaseErrorException: Cannot parse UUID parameter: "{"EXAMPLE"}"
  1. Empty array insertion
 const items: (typeof items_table.$inferInsert)[] = MOCK_ITEMS.map((x) => {
    return {
      ...x,
      type: x. type,
      tags: [],
    };
  });

  await pg.insert(items_table).values(items);

It gives this error:

DatabaseErrorException: Cannot parse UUID parameter: "{}"
  1. Enum array insertion
 const items: (typeof items_table.$inferInsert)[] = MOCK_ITEMS.map((x) => {
    return {
      ...x,
      type: x. type,
      tags: [ItemTag.Example],
    };
  });

  await pg.insert(items_table).values(items);

It gives this error:

DatabaseErrorException: Cannot parse UUID parameter: "{"EXAMPLE"}"

What worked

Hardcoding the value, which will be the same as what the sql.raw spits out, does work:

  const items: (typeof items_table.$inferInsert)[] = MOCK_ITEMS.map((x) => {
    return {
      ...x,
      type: x. type,
      //This is cursed AF, and its to make RDS work.
      tags: sql`string_to_array('EXAMPLE,TEST', ',')::text[]`,
    };
  });

The following works:

  const items: (typeof items_table.$inferInsert)[] = MOCK_ITEMS.map((x) => {
    return {
      ...x,
      type: x. type,
      //This is cursed AF, and its to make RDS work.
      tags: rdsArray(x. tags as ItemTag[]) as unknown as ItemTag[],
    };
  });
// Our function
function rdsArray(items: string[]): SQL<unknown> {
  // We know this exact format works when used as a single literal
  const fullSql = `string_to_array('${items.join(',')}', ',')::text[]`;
  return sql.raw(fullSql);
}
@amandaharlin amandaharlin added the bug Something isn't working label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant