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

PostgreSQL, text array, migrations. drizzle-kit generate produces invalid syntax in migration ALTER statement #400

Open
gburtini opened this issue May 13, 2024 · 1 comment

Comments

@gburtini
Copy link

An ALTER migration generated with drizzle-kit generate from a table schema like this:

pgTable("users", {
  ...
  permissions: text("permissions").array().notNull().default([]),
});

Produces a migration that looks like this:

ALTER TABLE "users" ADD COLUMN "permissions" text[] DEFAULT   NOT NULL;

Which produces an error in all recent Postgres versions I could test out including 16.3.

ERROR:  syntax error at or near "NOT"
LINE 1: ... "users" ADD COLUMN "permissions" text[] DEFAULT   NOT NULL;

The correct syntax would include a {} Postgres array literal.

@gburtini gburtini changed the title Postgres, array: drizzle-kit generate produces invalid syntax in migration ALTER statement PostgreSQL, text array, migrations. drizzle-kit generate produces invalid syntax in migration ALTER statement May 13, 2024
@gburtini
Copy link
Author

gburtini commented May 13, 2024

This appears to have been reported over here a year ago drizzle-team/drizzle-orm#1344 and closed as not a bug.

The types reported reject a pure string solution permissions: text("permissions").array().notNull().default("{}") and (IMO, correctly) indicate that the expected type of default is a string[].

That said, the prescribed tagged SQL string approach does work.

.default(sql`{}`)

That's a sufficient workaround, but it doesn't seem like an ideal solution.

  • the DDL types should reject an array if an array isn't the valid input here.
    • a better solution would be for Drizzle to mediate any difference in a JS array and a Postgres array.
  • drizzle-kit generate should fail, rather than produce invalid SQL, at least on inputs that do not involve any sql literals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant