drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG] MySQL relational query syntax error

Open doender opened this issue 1 year ago • 12 comments

What version of drizzle-orm are you using?

0.28.3

What version of drizzle-kit are you using?

0.19.5

Describe the Bug

Using MySQL and the following schema:

export const tickets = mysqlTable(
  "tickets",
  {
    uuid: varchar("uuid", { length: 36 }),
  },
  (table) => {
    return {
      uuid: index("uuid").on(table.uuid),
    };
  },
);

export const ticketsRelations = relations(tickets, ({ many }) => ({
  transferData: many(transferData),
}));

export const transferData = mysqlTable(
  "transfer_data",
  {
    id: bigint("id", { mode: "number" }).autoincrement().primaryKey().notNull(),
    ticketId: bigint("ticket_id", { mode: "number" }),
    data: json("data"),
  },
  (table) => {
    return {
      id: unique("id").on(table.id),
      ticketId: unique("ticket_id").on(table.ticketId),
    };
  },
);

export const transferDataRelations = relations(transferData, ({ one }) => ({
  ticket: one(tickets, {
    fields: [transferData.ticketId],
    references: [tickets.id],
  }),
}));

and the following query:

db.query.tickets.findMany({
      with: {
        transferData: true,
      },
      limit: 10,
    });

I get this syntax error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(select coalesce(json_arrayagg(json_array(`tickets_transferData`.`id`, `tickets_' at line 1

Expected behavior

No syntax error

Environment & setup

Google Cloud Platform CloudSQL, MySQL 5.7

doender avatar Aug 23 '23 15:08 doender