Drizzle-zod typeerror when passing in pgtable

I keep getting a type error when trying to get the zod schema from the pg schema with drizzle: public.ts
import { pgTable, timestamp, serial, text, pgEnum } from 'drizzle-orm/pg-core'

export const TeamSubscriptionTier = pgEnum('team_subscription_tier', ['basic', 'team', 'enterprise']);

export const teams = pgTable('teams', {
/**
* The auto-incrementing ID of the team.
*/
id: serial('team_id').primaryKey(),

/**
* The timestamp when the team was created.
*/
created_at: timestamp('created_at', { withTimezone: false }).defaultNow().notNull(),

/**
* The name of the team.
*/
name: text('name').notNull(),

/**
* The description of the team.
*/
description: text('description'),

/**
* The subscription tier of the team.
*/
subcription_tier: TeamSubscriptionTier('subscription_tier')
});
import { pgTable, timestamp, serial, text, pgEnum } from 'drizzle-orm/pg-core'

export const TeamSubscriptionTier = pgEnum('team_subscription_tier', ['basic', 'team', 'enterprise']);

export const teams = pgTable('teams', {
/**
* The auto-incrementing ID of the team.
*/
id: serial('team_id').primaryKey(),

/**
* The timestamp when the team was created.
*/
created_at: timestamp('created_at', { withTimezone: false }).defaultNow().notNull(),

/**
* The name of the team.
*/
name: text('name').notNull(),

/**
* The description of the team.
*/
description: text('description'),

/**
* The subscription tier of the team.
*/
subcription_tier: TeamSubscriptionTier('subscription_tier')
});
contract.ts
import { initContract } from '@ts-rest/core';
import { z } from 'zod';
import { ErrorResponseSchema } from '@bloxgrid/common/dist/schemas/error-response.schema'
import { teams } from '@bloxgrid/common/src/database/schema/public'
import { createSelectSchema } from 'drizzle-zod'

const c = initContract();

export const teamsContract = c.router({
getTeams: {
method: 'GET',
path: '/',
query: z.object({
name: z.string().optional(),
}),
responses: {
200: createSelectSchema(teams), //teams here is giving the type error
400: ErrorResponseSchema
},
summary: 'Get a list of teams',
}
}, {
pathPrefix: '/teams' as const
});
import { initContract } from '@ts-rest/core';
import { z } from 'zod';
import { ErrorResponseSchema } from '@bloxgrid/common/dist/schemas/error-response.schema'
import { teams } from '@bloxgrid/common/src/database/schema/public'
import { createSelectSchema } from 'drizzle-zod'

const c = initContract();

export const teamsContract = c.router({
getTeams: {
method: 'GET',
path: '/',
query: z.object({
name: z.string().optional(),
}),
responses: {
200: createSelectSchema(teams), //teams here is giving the type error
400: ErrorResponseSchema
},
summary: 'Get a list of teams',
}
}, {
pathPrefix: '/teams' as const
});
10 Replies
LuukOriginal
LuukOriginal4mo ago
The Type Error:
Type '{ id: PgColumn<{ name: "team_id"; tableName: "teams"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; created_at: PgColumn<...>; name: PgColumn<...>; description: PgColumn<...>; subcription_tier: PgC...' is not assignable to type 'Record<string, Column<any, object, object>>'.
Property 'id' is incompatible with index signature.
Type 'PgColumn<{ name: "team_id"; tableName: "teams"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>' is missing the following properties from type 'Column<any, object, object>': generated, generatedIdentity
Type '{ id: PgColumn<{ name: "team_id"; tableName: "teams"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; created_at: PgColumn<...>; name: PgColumn<...>; description: PgColumn<...>; subcription_tier: PgC...' is not assignable to type 'Record<string, Column<any, object, object>>'.
Property 'id' is incompatible with index signature.
Type 'PgColumn<{ name: "team_id"; tableName: "teams"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>' is missing the following properties from type 'Column<any, object, object>': generated, generatedIdentity
if any more information is required, feel free to ask
rphlmr ⚡
rphlmr ⚡4mo ago
👋 I can't reproduce. Are you on latest versions for both drizzle-zod and drizzle-orm? https://drizzle.run/k4lmz05tzibn4mesgb2t4ldu
LuukOriginal
LuukOriginal4mo ago
I tried updating my packages it and that seemed to have fixed the problem, thanks
LuukOriginal
LuukOriginal3mo ago
I do still seem to have a small problem in my contract, it seems like the id (serial) and created_at (timestamp) turn into enums instead of their respective types, I assume this not the expected behavior right?
const t = createSelectSchema(teams)
const t = createSelectSchema(teams)
gives:
No description
LuukOriginal
LuukOriginal3mo ago
@Raphaël M (@rphlmr) ⚡
No description
LuukOriginal
LuukOriginal3mo ago
schema and contract are still the same as the onces I sent before versions are:
drizzle-kit: 0.23.0
drizzle-orm: 0.32.0
drizzle-zod: 0.5.1
zod: 3.23.8
drizzle-kit: 0.23.0
drizzle-orm: 0.32.0
drizzle-zod: 0.5.1
zod: 3.23.8
it seems to work fine on drizzle run tho I found out, also tried deleting all node_modules folders and reinstalling everything, but that didn't work either
LuukOriginal
LuukOriginal3mo ago
also, the schema does work on the actual api, it does seem to be a problem with drizzle zod I guess?
No description
LuukOriginal
LuukOriginal3mo ago
the type of teams I am passing seems to be fine tho:
No description
rphlmr ⚡
rphlmr ⚡3mo ago
I can reproduce if I don't have:
"strict": true,
"strict": true,
in my tsconfig. It should fix your issue
LuukOriginal
LuukOriginal3mo ago
Oh, that seemed to work, now I can finally continue on working on the application, thank you so much! 😁 👍
Want results from more Discord servers?
Add your server