Jonathan
Jonathan
Explore posts from servers
DTDrizzle Team
Created by Jonathan on 5/10/2024 in #help
Incorrect relations
I have this Drizzle ORM query:
const projects = await ctx.db.query.syndicationProject.findMany({
with: {
entity: {
with: {
members: {
with: {
member: true,
},
},
},
},
},
});
const projects = await ctx.db.query.syndicationProject.findMany({
with: {
entity: {
with: {
members: {
with: {
member: true,
},
},
},
},
},
});
The problem is that "members" under each project.entity is empty, even though the correct rows and references are in the database. You can see how I have defined my schemas in my comment on this post. The weird thing is, if I change the relation name to "entity" like so:
export const personRelations = relations(person, ({ one, many }) => ({
members: many(membership, { relationName: "entity" }),
}));
export const personRelations = relations(person, ({ one, many }) => ({
members: many(membership, { relationName: "entity" }),
}));
Suddenly I do get the correct result. How can this be? The relation name should be "member", because I want to fetch the nested members. Or am I misunderstanding? Hopefully you can help me out!
4 replies
DTDrizzle Team
Created by Jonathan on 4/13/2024 in #help
How do I recursively fetch tables?
Hi all, I have two tables in my postgres database:
person {
id
}

person_to_person {
id
parent_id
child_id
}
person {
id
}

person_to_person {
id
parent_id
child_id
}
The parent_id and child_id are references to the person table. I am trying to create a family tree-like structure, which means that I need to fetch every child recursively until there are no children left to fetch. Imagine a family tree. If you know the id of any given member in that tree (could be anywhere in the family tree), fetch all of its children recursively all the way to the bottom where there are no more children left. How do I accomplish this with Drizzle ORM? Preferably I want the result to be typed, so that the front-end can easily show this family tree on a webpage. I hope you can help me out!
5 replies
DTDrizzle Team
Created by Jonathan on 8/1/2023 in #help
Drizzle kit SQL error
Hi all, I'm getting this weird error when I try to push my schema changes using drizzle-kit push and I have no idea where to begin to solve it:
Error: unknown: uncaught panic: interface conversion: interface is nil, not sqlparser.Expr, vtgate: http://
pscluster-xkj7v0lvx1yw-aws-uswest2a-2-vtgate-6cd24c1e-84962nl82:15000/
at PromiseConnection.execute (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\inde
x.cjs:35435:26)
at fromDatabase (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\index.cjs:11845:3
3)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async mysqlPushIntrospect (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\inde
x.cjs:37725:19)
at async Command.<anonymous> (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\inde
x.cjs:53103:31) {
code: 'ER_UNKNOWN_ERROR',
errno: 1105,
sql: 'SELECT table_name, column_name\n' +
' FROM information_schema.table_constraints t\n' +
' LEFT JOIN information_schema.key_column_usage k\n' +
' USING(constraint_name,table_schema,table_name)\n' +
" WHERE t.constraint_type='PRIMARY KEY'\n" +
" and table_name != '__drizzle_migrations'\n" +
' AND t.table_schema = ?',
sqlState: 'HY000',
sqlMessage: 'unknown: uncaught panic: interface conversion: interface is nil, not sqlparser.Expr, vtgate:
http://pscluster-xkj7v0lvx1yw-aws-uswest2a-2-vtgate-6cd24c1e-84962nl82:15000/'
}
Error: unknown: uncaught panic: interface conversion: interface is nil, not sqlparser.Expr, vtgate: http://
pscluster-xkj7v0lvx1yw-aws-uswest2a-2-vtgate-6cd24c1e-84962nl82:15000/
at PromiseConnection.execute (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\inde
x.cjs:35435:26)
at fromDatabase (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\index.cjs:11845:3
3)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async mysqlPushIntrospect (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\inde
x.cjs:37725:19)
at async Command.<anonymous> (C:\Users\jrrvd\Desktop\Projects\dcap-docgen\node_modules\drizzle-kit\inde
x.cjs:53103:31) {
code: 'ER_UNKNOWN_ERROR',
errno: 1105,
sql: 'SELECT table_name, column_name\n' +
' FROM information_schema.table_constraints t\n' +
' LEFT JOIN information_schema.key_column_usage k\n' +
' USING(constraint_name,table_schema,table_name)\n' +
" WHERE t.constraint_type='PRIMARY KEY'\n" +
" and table_name != '__drizzle_migrations'\n" +
' AND t.table_schema = ?',
sqlState: 'HY000',
sqlMessage: 'unknown: uncaught panic: interface conversion: interface is nil, not sqlparser.Expr, vtgate:
http://pscluster-xkj7v0lvx1yw-aws-uswest2a-2-vtgate-6cd24c1e-84962nl82:15000/'
}
I am using PlanetScale as my database provider.
12 replies
TTCTheo's Typesafe Cult
Created by Jonathan on 6/25/2023 in #questions
Linting errors when building on Vercel that I'm not getting on local machine
Hi all, My build on Vercel is failing because of linting errors. The weird thing is, I'm not getting these errors when building locally at all. This is an excerpt from the build error I'm getting on Vercel:
./src/server/api/trpc.ts
45:9 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment
./src/server/db.ts
8:14 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment
10:5 Error: Unsafe construction of an any type value. @typescript-eslint/no-unsafe-call
17:36 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment
info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
error Command failed with exit code 1.
./src/server/api/trpc.ts
45:9 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment
./src/server/db.ts
8:14 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment
10:5 Error: Unsafe construction of an any type value. @typescript-eslint/no-unsafe-call
17:36 Error: Unsafe assignment of an `any` value. @typescript-eslint/no-unsafe-assignment
info - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
error Command failed with exit code 1.
The errors seem to indicate that I am unsafely assigning values with type "any" and that I am unsafely accessing properties of those values in other files. These errors happen in files that are related to or use Prisma, but when I check these files, there are no "any" types in the files at all. For reference, here's the server/db.ts file that is giving errors:
import { PrismaClient } from "@prisma/client";
import { env } from "@/env.mjs";

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};

export const prisma =
globalForPrisma.prisma ??
new PrismaClient({
log:
env.NODE_ENV === "development"
? ["query", "error", "warn"]
: ["error"],
});

if (env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
import { PrismaClient } from "@prisma/client";
import { env } from "@/env.mjs";

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};

export const prisma =
globalForPrisma.prisma ??
new PrismaClient({
log:
env.NODE_ENV === "development"
? ["query", "error", "warn"]
: ["error"],
});

if (env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
When hovering over the variables in my editor (VS Code), everything seems to have a proper type. As mentioned before, the build does not fail on my local machine, but does on Vercel. I am asking this question here, because this project was generated using the create-t3-app cli. I hope someone can help me. Please let me know if this question is better to ask elsewhere.
2 replies