我正在尝试学习 Drizzle,当我尝试执行查询时遇到此错误,但是当我运行代码时,我收到此错误
src/main.ts:7:35 - 错误 TS2339:属性“UserTable”不存在 在类型“DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">”上。
7 const 结果=等待db.query.UserTable.findMany({
main.ts
import { db } from "./db/db";
import { UserTable } from "./db/schema/schema";
async function main(){
const result = await db.query.UserTable.findMany({
with: {
name: true
},
});
console.log(result)
}
main()
架构.ts
export const UserTable = mysqlTable('users',{
id: int('id').primaryKey().autoincrement(),
name: varchar('name', {length: 255}).notNull(),
age: int("age").notNull(),
email: varchar("email", {length: 255}).notNull(),
roll: mysqlEnum('userRole',["ADMIN","BASIC"]).default("BASIC").notNull(),
})
尝试这样的事情
import * as schema from "./schema"
const db = drizzle(mysqlConnection, { schema }) as MySqlDatabase<typeof schema>;
如果你的所有模式都在一个文件中,你应该能够像这样导入它并使用你的驱动程序的通用(似乎你正在使用mysql)来告诉打字稿你的数据库看起来如何