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

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);
}
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...
18 Replies
Michael Schaufelberger
Partial error stack:
TypeError: Cannot read properties of undefined (reading 'name')
at file:///path_to_repo/node_modules/.pnpm/[email protected]_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/[email protected]_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:7:3788)
at QueryPromise.getSQL (file:///path_to_repo/node_modules/.pnpm/[email protected]_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:1:3874)
at QueryPromise.toSQL (file:///path_to_repo/node_modules/.pnpm/[email protected]_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/@[email protected]/node_modules/@trpc/server/dist/index.mjs:416:30)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async resolve (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:482:24)
TypeError: Cannot read properties of undefined (reading 'name')
at file:///path_to_repo/node_modules/.pnpm/[email protected]_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/[email protected]_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:7:3788)
at QueryPromise.getSQL (file:///path_to_repo/node_modules/.pnpm/[email protected]_x5zvu6yhqtclhr76ddvo6x3wgi/node_modules/drizzle-orm/session-8a621f09.mjs:1:3874)
at QueryPromise.toSQL (file:///path_to_repo/node_modules/.pnpm/[email protected]_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/@[email protected]/node_modules/@trpc/server/dist/index.mjs:416:30)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async callRecursive (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:452:32)
at async resolve (file:///path_to_repo/node_modules/.pnpm/@[email protected]/node_modules/@trpc/server/dist/index.mjs:482:24)
Andrii Sherman
Can you show a code where you creating db?
Michael Schaufelberger
yes 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'),
});
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'),
});
Andrii Sherman
I mean db = drizzle(…)
Michael Schaufelberger
ah, it's planetscale
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);
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);
Andrii Sherman
do you have monorepo project? do you have 1 or multiple drizzle-orm instances installed?
Michael Schaufelberger
i have a monorepo but it's the first app in it with drizzle-orm other inserts seem to work fine. but i think i'll check again. also, mysql:push is working yep, just tried the following insert and that works:
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
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
Andrii Sherman
okey, so another table is working I have 1 more question, could you please check if you have imported serial, int, etc, from /mysql-core for ticketInvitations table
Michael Schaufelberger
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';
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';
a single file per schema. with zod schemas in it. (same as with the guests schema, just different name)
Andrii Sherman
I can't see a reason why it breaks cc @Dan Kochetov maybe you can check when you'll have some time? oh
Michael Schaufelberger
ah, and i've tried @beta in the meantime. same error
Andrii Sherman
I see, that in first example you are using array and in second just an object could you check to run first query(that you have troubles with) without an array just to check if array causing it
Michael Schaufelberger
ah, yeah. i've tried values[0] as well but i'll quickly try again
const insert = values[0];
console.log('insert', insert);
try {
const sql = ctx.db.insert(ticketInvitations).values(insert).toSQL();
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')
insert {
guestId: 1,
ticketTypeId: '6458eeb763aee7ab82c8b79b',
eventId: 1,
amount: 1
}
TypeError: Cannot read properties of undefined (reading 'name')
Dan
Dan2y ago
Could you try installing drizzle-orm in the monorepo root using -w?
Michael Schaufelberger
i just found the issue like in this second. i'm stupid and forgot to define the amount column 🤦‍♂️ I'm so so sorry
Dan
Dan2y ago
That's ok We actually need to improve the types there by not allowing unknown keys Glad it's sorted out
Michael Schaufelberger
I'm so getting used to typesafety with this project (trpc, drizzle, zod). And the error message doesn't mention amount. But stricter typing will easily be enough. Again: Sorry!
lanc3
lanc311mo ago
Just a small bump on this since I also encountered this issue and was confused for a while for the same reasons as OP. This seems to be a typescript issue? I.e. in my case this gives no error
await db
.update(workflows)
.set(input) // Drizzle zod schema validated but with relations (which was my issue). No type errors.
.where(eq(workflows.id, input.id));
await db
.update(workflows)
.set(input) // Drizzle zod schema validated but with relations (which was my issue). No type errors.
.where(eq(workflows.id, input.id));
but the following gives the correct type error
const {relation1, relation2, ...rest} = input;
await db
.update(workflows)
.set({relation1, relation2, ...rest}) // Error: Object literal may only specify known properties, and relation1 does not exist in type
.where(eq(workflows.id, rest.id));
const {relation1, relation2, ...rest} = input;
await db
.update(workflows)
.set({relation1, relation2, ...rest}) // Error: Object literal may only specify known properties, and relation1 does not exist in type
.where(eq(workflows.id, rest.id));
and removing the relation fields fixes both the type error and the error from drizzle. Perhaps then the best solution is to handle this error and throw a descriptive error from drizzle
Want results from more Discord servers?
Add your server