Skip to content

[BUG]: drizzle-kit push fails if the target postgres database has a jsonb column with a default value #4529

@ssg-saad

Description

@ssg-saad

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.43.1

What version of drizzle-kit are you using?

0.31.1

Other packages

No response

Describe the Bug

What is the undesired behavior?

> drizzle-kit push --strict --verbose

No config path provided, using default 'drizzle.config.ts'
Reading config file '/Users/<user>/<project>/drizzle.config.ts'
Using 'pg' driver for database querying
[⣽] Pulling schema from database...
{
  column11: {
    table_name: 'messages',
    column_name: 'attachments',
    is_nullable: 'NO',
    array_dimensions: 1,
    data_type: 'jsonb[]',
    seq_name: null,
    column_default: "'{}'",
    additional_dt: 'ARRAY',
    enum_name: '_jsonb',
    is_generated: 'NEVER',
    generation_expression: null,
    is_identity: 'NO',
    identity_generation: null,
    identity_start: null,
    identity_increment: null,
    identity_maximum: null,
    identity_minimum: null,
    identity_cycle: 'NO',
    type_schema: 'pg_catalog'
  },
  value: ''
}
<anonymous_script>:1


SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /Users/<user>/<project>/node_modules/drizzle-kit/bin.cjs:19970:66
    at Array.map (<anonymous>)
    at defaultForColumn (/Users/<user>/<project>/node_modules/drizzle-kit/bin.cjs:19959:74)
    at /Users/<user>/<project>/node_modules/drizzle-kit/bin.cjs:19565:36
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Node.js v23.10.0

The object output after "Pulling schema from database..." is from a console.log statement I added to drizzle-kit/bin.cjs to try to debug the issue. You can see it in the following code snippet:

      if (isArray3) {
        return `'{${columnDefaultAsString.slice(2, -2).split(/\s*,\s*/g).map((value) => {
          if (["integer", "smallint", "bigint", "double precision", "real"].includes(column11.data_type.slice(0, -2))) {
            return value;
          } else if (column11.data_type.startsWith("timestamp")) {
            return `${value}`;
          } else if (column11.data_type.slice(0, -2) === "interval") {
            return value.replaceAll('"', `"`);
          } else if (column11.data_type.slice(0, -2) === "boolean") {
            return value === "t" ? "true" : "false";
          } else if (["json", "jsonb"].includes(column11.data_type.slice(0, -2))) {
            console.log({column11, value}) // fails for this column and value
            return JSON.stringify(JSON.stringify(JSON.parse(JSON.parse(value)), null, 0));
          } else {
            return `"${value}"`;
          }
        }).join(",")}}'`;
      }

Table definition:

create table public.messages (
  id uuid not null default gen_random_uuid (),
  chat_id uuid null,
  content text not null,
  role public.role not null,
  created_at timestamp without time zone not null default now(),
  hidden boolean not null default false,
  attachments jsonb[] not null default '{}'::jsonb[],
  constraint messages_pkey primary key (id),
  constraint messages_chat_id_chats_id_fk foreign KEY (chat_id) references chats (id) on update CASCADE on delete CASCADE
) TABLESPACE pg_default;

What are the steps to reproduce it?

  1. Create a table with a jsonb column that has default value of '{}'::jsonb[] on your target database
  2. Run drizzle-kit push

What is the desired result?

It should correctly parse the default value

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbug/fixed-in-betaThis bug has been fixed in beta (or will be soon).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions