DT
Drizzle Team•13mo ago
Zephury

I swear this infer was working... am I crazy?

export type User = typeof user.$inferSelect;

export const user = pgTable('user', {
id: bigint('id', { mode: 'bigint' }).primaryKey(),
...rest of table
export type User = typeof user.$inferSelect;

export const user = pgTable('user', {
id: bigint('id', { mode: 'bigint' }).primaryKey(),
...rest of table
I swear for a few days, the exported User type was working just fine. Suddenly, it is always "any" am I remembering wrong, or is this supposed to work?
10 Replies
George
George•13mo ago
try restarting ur ts server
Angelelz
Angelelz•13mo ago
I I've seem this error when people uses a reference to the table itself on the 3rd parameter while trying to setup foreign keys
Zephury
ZephuryOP•13mo ago
I tried deleting all caches, restarting vs code, restarting ts server and still, no luck 🥲 So it seems its just something I’ll have to wait and hope for an eventual fix with? I’m not sure exactly what you mean by the 3rd parameter. Could you give me a rough example? I suppose it did happen around the time I was messing with relations and inferred relations
Angelelz
Angelelz•13mo ago
Can you show your table definitions?
Zephury
ZephuryOP•13mo ago
export enum UserRole {
MANAGER = 'manager',
MAINTAINER = 'maintainer',
AUTHOR = 'author',
TRANSLATOR = 'translator',
MEMBER = 'member',
USER = 'user',
}

export type User = typeof user.$inferSelect;

export const user = pgTable('user', {
id: bigint('id', { mode: 'bigint' }).primaryKey(),
profileId: bigint('profile_id', { mode: 'bigint' })
.unique()
.references(() => profile.id),
discordId: varchar('discord_id', { length: 256 }),
discordUsername: varchar('discord_username', { length: 256 }),
discordDiscriminator: varchar('discord_discriminator', { length: 256 }),
discordGlobalName: varchar('discord_global_name', { length: 256 }),
discordAccessToken: varchar('discord_access_token', { length: 256 }),
discordRefreshToken: varchar('discord_refresh_token', { length: 256 }),
discordAvatar: varchar('discord_avatar', { length: 256 }),
roles: json('role').default(['user']).notNull().$type<UserRole[]>(),
isAdmin: boolean('is_admin').default(false),
isBanned: boolean('is_banned').default(false),
editLanguages: json('edit_languages'),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});

export const userRelations = relations(user, ({ one }) => ({
profile: one(profile, {
fields: [user.profileId],
references: [profile.id],
}),
}));
export enum UserRole {
MANAGER = 'manager',
MAINTAINER = 'maintainer',
AUTHOR = 'author',
TRANSLATOR = 'translator',
MEMBER = 'member',
USER = 'user',
}

export type User = typeof user.$inferSelect;

export const user = pgTable('user', {
id: bigint('id', { mode: 'bigint' }).primaryKey(),
profileId: bigint('profile_id', { mode: 'bigint' })
.unique()
.references(() => profile.id),
discordId: varchar('discord_id', { length: 256 }),
discordUsername: varchar('discord_username', { length: 256 }),
discordDiscriminator: varchar('discord_discriminator', { length: 256 }),
discordGlobalName: varchar('discord_global_name', { length: 256 }),
discordAccessToken: varchar('discord_access_token', { length: 256 }),
discordRefreshToken: varchar('discord_refresh_token', { length: 256 }),
discordAvatar: varchar('discord_avatar', { length: 256 }),
roles: json('role').default(['user']).notNull().$type<UserRole[]>(),
isAdmin: boolean('is_admin').default(false),
isBanned: boolean('is_banned').default(false),
editLanguages: json('edit_languages'),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});

export const userRelations = relations(user, ({ one }) => ({
profile: one(profile, {
fields: [user.profileId],
references: [profile.id],
}),
}));
Angelelz
Angelelz•13mo ago
I don't see anything wrong with your table definition. Since this is a tested API, the only way to help you is with a reproduction repository... Please create one and I'll take a look
Zephury
ZephuryOP•13mo ago
thank you. I will create one as soon as I'm able to find the time https://github.com/drizzle-team/drizzle-orm/issues/1502 this is exactly the issue I have, for reference
Angelelz
Angelelz•13mo ago
Interesting, does using the .reference method on the column work?
Zephury
ZephuryOP•13mo ago
everything seems to work. just no types. I haven't tested extensively, but I haven't had any actual problems. If I understood correctly, drizzle-kit studio needs to have the relations, as you see in userRelations, from my example in order to work though? I was not able to use drizzle-kit studio unless I provided the userRelations. I believe that is the expected behavior though?
Angelelz
Angelelz•13mo ago
Yes, the nice relational API require you to define the relations. But the reference is to create foreign keys at the db level
Want results from more Discord servers?
Add your server