Add even when object is null?

Hi guys, I have some issues with my schema:
export const userItems = pgTable("userItem", {
product_id: serial("product_id").notNull(),
user_id: text("user_id").notNull(),
quantity: integer("quantity")
},
(userItems) => ({
compoundKey: primaryKey(userItems.product_id, userItems.user_id),
}
)
)

export const userItemsRelations = relations(userItems, ({one}) => ({
product: one(products, {
fields: [userItems.product_id],
references: [products.id]
}),

user: one(users, {
fields: [userItems.user_id],
references: [users.id]
}),
}))
export const userItems = pgTable("userItem", {
product_id: serial("product_id").notNull(),
user_id: text("user_id").notNull(),
quantity: integer("quantity")
},
(userItems) => ({
compoundKey: primaryKey(userItems.product_id, userItems.user_id),
}
)
)

export const userItemsRelations = relations(userItems, ({one}) => ({
product: one(products, {
fields: [userItems.product_id],
references: [products.id]
}),

user: one(users, {
fields: [userItems.user_id],
references: [users.id]
}),
}))
You can see a small part of my schema. In fact, I can add a user item even when the product_id, that refers to my product id, doesn't exist. How can I tackle this issue ? I should check before the insert in my request? Thanks
5 Replies
wailroth
wailrothOP15mo ago
up? :p
Revan
Revan15mo ago
try user_id: text("user_id").notNull().references(() => users.id), and same for products
wailroth
wailrothOP15mo ago
on userItems ?
Revan
Revan15mo ago
yeah
wailroth
wailrothOP15mo ago
yeah it works thanks mate
Want results from more Discord servers?
Add your server