我有一个实体
// schema.ts
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', {length: 256}).notNull(),
});
然后我尝试初始化 drizzle 客户端
// db.server.ts
import {PostgresJsDatabase, drizzle} from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import * as schema from './schema';
const queryClient = postgres("postgres://...");
const db: PostgresJsDatabase = drizzle(queryClient, {schema});
VS Code 开始抱怨 {schema} 部分并出现以下错误:
Type 'PostgresJsDatabase<typeof import("/app/db/schema")>' is not assignable to type 'PostgresJsDatabase'.
The types of '_.schema' are incompatible between these types.
Type 'ExtractTablesWithRelations<typeof import(/app/db/schema")> | undefined' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>> | undefined'.
Type 'ExtractTablesWithRelations<typeof import("/app/db/schema")>' is not assignable to type 'ExtractTablesWithRelations<Record<string, never>>'.
Property 'users' is incompatible with index signature.
Type '{ tsName: "users"; dbName: "users"; columns: { id: PgColumn<{ name: "id"; tableName: "users"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; email: PgColumn<...>; }; relations: never; primaryKey: An...' is not assignable to type '{ tsName: string; dbName: never; columns: never; relations: Record<string, Relation<string>>; primaryKey: AnyColumn[]; }'.
Types of property 'dbName' are incompatible.
Type 'string' is not assignable to type 'never'.ts(2322)
我从来都不是 Typescript 的朋友(我还在尝试),所以我不知道出了什么问题。这就是他们在示例中的做法...有什么建议吗?
如果我保留
{schema}
部分,效果就很好,但我想我需要它来使用我想使用的 db.query.users.findMany()
语法?
db
常量的转换是错误的。它缩小了结果范围。因此,删除 PostgresJsDatabase
演员阵容可以解决问题。