MySQL (Planetscale): Cannot read properties of undefined (reading 'name')

Mmichaelschufi5/12/2023
Hi

Hopefully someone can help me. I'm getting the above error in my insert query.

try {
  const sql = ctx.db.insert(ticketInvitations).values([
    {
      guestId: 1,
      ticketTypeId: '6458eeb763aee9ab82d8b79b',
      eventId: 1,
      amount: 1,
      status: 'pending' as const,
    }
  ]).toSQL();
  console.log(sql);
} catch (error) {
  console.error(error);
}


I have no idea what I'm doing wrong...
Mmichaelschufi5/12/2023
Partial error stack:

TypeError: Cannot read properties of undefined (reading 'name')
    at file:///path_to_repo/node_modules/.pnpm/drizzle-orm@0.25.4_x5zvu6yhqtclhFull error stack:

r76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:7:3805
    at Array.map (<anonymous>)
    at Y.buildInsertQuery (file:///path_to_repo/node_modules/.pnpm/drizzle-orm@0.25.4_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:7:3788)
    at QueryPromise.getSQL (file:///path_to_repo/node_modules/.pnpm/drizzle-orm@0.25.4_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:1:3874)
    at QueryPromise.toSQL (file:///path_to_repo/node_modules/.pnpm/drizzle-orm@0.25.4_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:1:3963)
    at eval (webpack-internal:///(api)/./src/modules/guests/router.ts:159:140)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async resolveMiddleware (file:///path_to_repo/node_modules/.pnpm/@trpc+server@10.25.0/node_modules/@trpc/server/dist/index.mjs:416:30)
    at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@trpc+server@10.25.0/node_modules/@trpc/server/dist/index.mjs:452:32)
    at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@trpc+server@10.25.0/node_modules/@trpc/server/dist/index.mjs:452:32)
    at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@trpc+server@10.25.0/node_modules/@trpc/server/dist/index.mjs:452:32)
    at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@trpc+server@10.25.0/node_modules/@trpc/server/dist/index.mjs:452:32)
    at async resolve (file:///path_to_repo/node_modules/.pnpm/@trpc+server@10.25.0/node_modules/@trpc/server/dist/index.mjs:482:24)
ASAndrii Sherman5/12/2023
Can you show a code where you creating db?
Mmichaelschufi5/12/2023
yes
Mmichaelschufi5/12/2023
Sorry, had to split because of message limits.
export const ticketInvitations = mysqlTable('ticketInvitations', {
  id: serial('id').primaryKey(),
  guestId: int('guestId').notNull(),
  eventId: int('eventId').notNull(),
  ticketTypeId: varchar('ticketTypeId', { length: 256 }).notNull(),
  // status: mysqlEnum('status', ['pending', 'accepted', 'declined']).notNull(),
  // expiresAt: datetime('expiresAt'),
});
ASAndrii Sherman5/12/2023
I mean db = drizzle(…)
Mmichaelschufi5/12/2023
ah, it's planetscale
Mmichaelschufi5/12/2023
import { connect } from '@planetscale/database';
import { drizzle } from 'drizzle-orm/planetscale-serverless';

const connection = connect({
  host: process.env.DATABASE_HOST,
  username: process.env.DATABASE_USERNAME,
  password: process.env.DATABASE_PASSWORD,
});

export const db = drizzle(connection);
ASAndrii Sherman5/12/2023
do you have monorepo project?
do you have 1 or multiple drizzle-orm instances installed?
Mmichaelschufi5/12/2023
i have a monorepo but it's the first app in it with drizzle-orm
Mmichaelschufi5/12/2023
other inserts seem to work fine. but i think i'll check again.

also, mysql:push is working
Mmichaelschufi5/12/2023
yep, just tried the following insert and that works:
Mmichaelschufi5/12/2023
ctx.db.insert(guests).values({ ...input, owner: ctx.identity.id });

// where input is of shape:
type input = {
    firstName: string;
    lastName: string;
    email: string;
    phone: string | null;
}
// and owner is a string
ASAndrii Sherman5/12/2023
okey, so another table is working
ASAndrii Sherman5/12/2023
I have 1 more question, could you please check if you have imported serial, int, etc, from /mysql-core for ticketInvitations table
Mmichaelschufi5/12/2023
the imports:
import type { InferModel } from 'drizzle-orm';
import {
  datetime,
  int,
  mysqlEnum,
  mysqlTable,
  serial,
  varchar,
} from 'drizzle-orm/mysql-core';
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';
import { z } from 'zod';
Mmichaelschufi5/12/2023
a single file per schema. with zod schemas in it. (same as with the guests schema, just different name)
ASAndrii Sherman5/12/2023
I can't see a reason why it breaks

cc @Dan Kochetov maybe you can check when you'll have some time?
ASAndrii Sherman5/12/2023
oh
Mmichaelschufi5/12/2023
ah, and i've tried @beta in the meantime. same error
ASAndrii Sherman5/12/2023
I see, that in first example you are using array and in second just an object
ASAndrii Sherman5/12/2023
could you check to run first query(that you have troubles with) without an array
ASAndrii Sherman5/12/2023
just to check if array causing it
Mmichaelschufi5/12/2023
ah, yeah. i've tried values[0] as well
but i'll quickly try again
Mmichaelschufi5/12/2023
      const insert = values[0];
      console.log('insert', insert);
      try {
        const sql = ctx.db.insert(ticketInvitations).values(insert).toSQL();

same issue
insert {
  guestId: 1,
  ticketTypeId: '6458eeb763aee7ab82c8b79b',
  eventId: 1,
  amount: 1
}
TypeError: Cannot read properties of undefined (reading 'name')
Bbloberenober5/12/2023
Could you try installing drizzle-orm in the monorepo root using -w?
Mmichaelschufi5/12/2023
i just found the issue
Mmichaelschufi5/12/2023
like in this second.
i'm stupid and forgot to define the amount column 🤦‍♂️
Mmichaelschufi5/12/2023
I'm so so sorry
Bbloberenober5/12/2023
That's ok
We actually need to improve the types there by not allowing unknown keys
Bbloberenober5/12/2023
Glad it's sorted out
Mmichaelschufi5/12/2023
I'm so getting used to typesafety with this project (trpc, drizzle, zod).
Mmichaelschufi5/12/2023
And the error message doesn't mention amount. But stricter typing will easily be enough.

Again: Sorry!