deved9036
DTDrizzle Team
•Created by deved9036 on 1/2/2024 in #help
Query from 2 Tables
Heya there, I am currently trying to query a product, I basically have a one to many setup with product and images and a many to many with products and tags. So essentially I want ot query the product with the images and tags together. When I add productTags: true, it seems to break the whole query. Any clues 👀
``js
export const products = pgTable("products", {
id: serial("id").primaryKey(),
description: text("description").notNull(),
price: real("price"),
title: text("title").notNull(),
color: text("color").notNull(),
subtitle: text("subtitle").notNull(),
})
export const productImages = pgTable("productImages", {
id: serial("id").primaryKey(),
url: text("image").notNull(),
size: real("size").notNull(),
name: text("name").notNull(),
key: text("key").notNull(),
productID: integer("productID")
.notNull()
.references(() => products.id, { onDelete: "cascade" }),
})
export const productTags = pgTable("productTags", {
id: serial("id").primaryKey(),
tag: text("tag").notNull(),
productID: integer("productID")
.notNull()
.references(() => products.id, { onDelete: "cascade" }),
})
export const productImagesRelations = relations(productImages, ({ one }) => ({
product: one(products, {
fields: [productImages.productID],
references: [products.id],
}),
}))
export const productTagRelations = relations(productTags, ({ many }) => ({
products: many(products),
}))
export const productRelations = relations(products, ({ many }) => ({
productImages: many(productImages),
productTags: many(productTags),
}))
//Get individual product
export async function getProduct(id: number) {
try {
const productData = await db.query.products.findMany({
where: eq(products.id, id),
with: {
productImages: true,
productTags: true //Shows up in intellisense but breaks
},
})
return { productData }
} catch (error) {
return { error:
Can't load product 😵` }
}
}5 replies
DTDrizzle Team
•Created by deved9036 on 10/5/2023 in #help
One to Many problem
I am trying to make a form that let's you add a product, with different variants. Example Product: Journal, Variants (blue journal, red journal). So I made two schemas and enforced a one-to-many relation. Now the problem is...I can create a product without having a variation. What would be a good solution for this, where a user needs to upload at least one variant, but can also have an array of variants to be uploaded?
1 replies
DTDrizzle Team
•Created by deved9036 on 9/23/2023 in #help
Easiest way to add an array of objects?
Hey, I am trying to add an array of objects to my schema with postgres, how can I do this? there seems to be a pgArray but can't find a pgObject 👀
export const products = pgTable("products", {
id: serial("id").primaryKey().unique(),
description: text("description").notNull(),
details: text("details").notNull(),
price: numeric("price").notNull(),
images: array(
pgObject({
title: text("title").notNull(),
tag: text("tag").notNull(),
imageUrl: text("imageUrl").notNull(),
}),
),
});
8 replies