Query API relation null type

Hi I am using query API to fetch data. I defined my schema. My schema is something like that
export const messages = pgTable('messages', {
id: uuid('id').primaryKey().defaultRandom(),
chatId: uuid('chat_id')
.references(() => chats.id, { onDelete: 'cascade' })
.notNull(),
senderId: uuid('sender_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
type: messageTypeEnum('type').notNull().default('default'),
text: text('text'),
image: text('image'),
video: text('video'),
audio: text('audio'),
sent: boolean('sent').notNull().default(true),
received: boolean('received').notNull().default(false),
liked: boolean('liked').notNull().default(false),
createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at')
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
deletedAt: timestamp('deleted_at')
})

export const messageRelations = relations(messages, ({ one }) => ({
user: one(users, { fields: [messages.senderId], references: [users.id] }),
chat: one(chats, { fields: [messages.chatId], references: [chats.id] }),
media: one(chatMedia, { fields: [messages.id], references: [chatMedia.messageId] })
}))

export const chatMedia = pgTable(
'chat_media',
{
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('userId')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
messageId: uuid('message_id')
.references(() => messages.id, { onDelete: 'cascade' })
.notNull(),
bucket: varchar('bucket', { length: 100 }).notNull(),
key: text('key').notNull(),
location: text('location').notNull()
},
(table) => ({
chatMediaUniqueUserIdMessageIdIdx: uniqueIndex().on(table.messageId, table.userId),
chatMediaUserIdIdx: index().on(table.userId)
})
)

export const chatMediaRelations = relations(chatMedia, ({ one }) => ({
user: one(users, { fields: [chatMedia.userId], references: [users.id] }),
message: one(messages, { fields: [chatMedia.messageId], references: [messages.id] })
}))
export const messages = pgTable('messages', {
id: uuid('id').primaryKey().defaultRandom(),
chatId: uuid('chat_id')
.references(() => chats.id, { onDelete: 'cascade' })
.notNull(),
senderId: uuid('sender_id')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
type: messageTypeEnum('type').notNull().default('default'),
text: text('text'),
image: text('image'),
video: text('video'),
audio: text('audio'),
sent: boolean('sent').notNull().default(true),
received: boolean('received').notNull().default(false),
liked: boolean('liked').notNull().default(false),
createdAt: timestamp('created_at').notNull().defaultNow(),
updatedAt: timestamp('updated_at')
.notNull()
.defaultNow()
.$onUpdate(() => new Date()),
deletedAt: timestamp('deleted_at')
})

export const messageRelations = relations(messages, ({ one }) => ({
user: one(users, { fields: [messages.senderId], references: [users.id] }),
chat: one(chats, { fields: [messages.chatId], references: [chats.id] }),
media: one(chatMedia, { fields: [messages.id], references: [chatMedia.messageId] })
}))

export const chatMedia = pgTable(
'chat_media',
{
id: uuid('id').primaryKey().defaultRandom(),
userId: uuid('userId')
.references(() => users.id, { onDelete: 'cascade' })
.notNull(),
messageId: uuid('message_id')
.references(() => messages.id, { onDelete: 'cascade' })
.notNull(),
bucket: varchar('bucket', { length: 100 }).notNull(),
key: text('key').notNull(),
location: text('location').notNull()
},
(table) => ({
chatMediaUniqueUserIdMessageIdIdx: uniqueIndex().on(table.messageId, table.userId),
chatMediaUserIdIdx: index().on(table.userId)
})
)

export const chatMediaRelations = relations(chatMedia, ({ one }) => ({
user: one(users, { fields: [chatMedia.userId], references: [users.id] }),
message: one(messages, { fields: [chatMedia.messageId], references: [messages.id] })
}))
query response type says media is always an object, but it can be null if there is no media data
No description
No description
3 Replies
Aaroned
Aaroned9mo ago
@AuroPick since you are defining a one-to-one relationship check out this page https://orm.drizzle.team/docs/rqb#one-to-one Note with the second example it says
Another example would be a user having a profile information stored in separate table. In this case, because the foreign key is stored in the “profile_info” table, the user relation have neither fields or references. This tells Typescript that user.profileInfo is nullable
So you can simply define the relationship as: media: one(chatMedia)
AuroPick
AuroPickOP9mo ago
Hi @Aaroned, thanks, are chat media relations correct? I do not need to change them either, do I?
Aaroned
Aaroned9mo ago
Yes chat media relations is correct
Want results from more Discord servers?
Add your server