Don
Don
DTDrizzle Team
Created by Don on 12/23/2023 in #help
Property 'employees' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is miss
I am getting an error trying to do a findmany on a table. Property 'employees' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">' Not sure what I have misconfigured. Here is the query: import { MySql2Database } from "drizzle-orm/mysql2"; import {employees, employee_strength, strengths} from "@/models/database" export async function GET(req: Request, res: NextApiResponse) {
const result = await db.query.employees.findMany({. <---- Employees is flagged as an error where: eq(employees.id, 11), ..... Here is the employees table in the file /models/database. There are not errors arising here. export const employees = mysqlTable("employees", { id: serial("id").notNull().primaryKey().references((): AnyMySqlColumn => employee_strength.employee_id), first_name: varchar("first_name", { length: 255 }), last_name: varchar("last_name", { length: 255 }), email: varchar("email", { length: 255 }), job_title: varchar("job_title", { length: 255 }), department_id: bigint("department_id", { mode: "number" }), organization_id: bigint("organization_id", { mode: "number" }), created_at: timestamp("created_at", { mode: "date", fsp: 6 }), updated_at: timestamp("updated_at", { mode: "date", fsp: 6 }), deleted_at: timestamp("deleted_at", { mode: "date", fsp: 6 }), }); Here is the config: import { drizzle } from "drizzle-orm/mysql2"; import mysql from "mysql2/promise"; import * as schema from '@/models/database'; import { int, serial, text, varchar, bigint, timestamp, mysqlTable, } from "drizzle-orm/mysql-core"; const poolConnection = mysql.createPool({ host: "127.0.0.1", user: "root", database: "partner2learn", }); export const db = drizzle(poolConnection); Thanks in advance for insight!
4 replies
DTDrizzle Team
Created by Don on 12/21/2023 in #help
Drizzle MySQL Many to Many relations
I am trying to define a many to many relation ship between the employees and strengths tables.
I am getting the error on the employeeStrengthRelations definition. Type 'any[]' is not assignable to type 'Record<string, Relation<any>>'. Index signature for type 'string' is missing in type 'any[]'.ts(2322) Any insight would be greatly appreciated. I want to query employees and get their strengths (title, not the id) in the query. Here are the table definitions. export const employees = mysqlTable('employees', { id: serial('id').notNull().primaryKey(), ..... }); export const strengths = mysqlTable('strengths', { id: serial('id').notNull().primaryKey(), title: varchar('title', { length: 255 }), }); export const employee_strength = mysqlTable('employee_strength', { id: serial('id').notNull().primaryKey(), employee_id: bigint('employee_id', { mode: 'number' }).references(() => employees.id), strength_id: bigint('strength_id', { mode: 'number' }).references(() => strengths.id), order: int('order'), }); export const employeeRelations = relations(employees, ({ many }) => ({ employeesToStrengths: many(employee_strength), })); export const strengthsRelations = relations(strengths, ({ many }) => ({ employeesToStrengths: many(employee_strength), })); This definition is the one giving the error export const employeeStrengthRelations = relations(employee_strength, ({ one }) => ([ strength: one(strengths, { fields: [employee_strength.strength_id], references: [strengths.id], }), employee: one(employees, { fields: [employee_strength.employee_id], references: [employees.id], }), ])); Here is the query, in which I want to get the employees with an array of their strength.titles in order using the order on employee_strength table. const results = await this.db.query.employees.findMany({ with: { employeesToStrengths: true, }, }); Thanks in advance for any insight.
43 replies