deved9036
deved9036
DTDrizzle Team
Created by deved9036 on 2/28/2024 in #help
Nested insert not working
No description
1 replies
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?
import { InferSelectModel, relations } from "drizzle-orm";
import {
pgTable,
serial,
text,
timestamp,
real,
integer,
} from "drizzle-orm/pg-core";

export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
image: text("image").notNull(),
clerkId: text("clerkId").notNull().unique(),
stripeCustomer: text("stripeCustomer").notNull().unique(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
});

export const products = pgTable("products", {
id: serial("id").primaryKey(),
description: text("description").notNull(),
price: real("price").notNull(),
title: text("title").notNull(),
subtitle: text("subtitle").notNull(),
});

export const productVariant = pgTable("productVariant", {
id: serial("id").primaryKey(),
image: text("image").notNull(),
color: text("color").notNull(),
variantName: text("variantName").notNull(),
productID: integer("productID").references(() => products.id),
});

export const productVariantRelations = relations(productVariant, ({ one }) => ({
product: one(products, {
fields: [productVariant.productID],
references: [products.id],
}),
}));

export const productRelations = relations(products, ({ many }) => ({
productVariants: many(productVariant),
}));

export type Products = InferSelectModel<typeof users>;
export type ProductVariants = InferSelectModel<typeof productVariant>;
export type User = InferSelectModel<typeof users>;
import { InferSelectModel, relations } from "drizzle-orm";
import {
pgTable,
serial,
text,
timestamp,
real,
integer,
} from "drizzle-orm/pg-core";

export const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
image: text("image").notNull(),
clerkId: text("clerkId").notNull().unique(),
stripeCustomer: text("stripeCustomer").notNull().unique(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
});

export const products = pgTable("products", {
id: serial("id").primaryKey(),
description: text("description").notNull(),
price: real("price").notNull(),
title: text("title").notNull(),
subtitle: text("subtitle").notNull(),
});

export const productVariant = pgTable("productVariant", {
id: serial("id").primaryKey(),
image: text("image").notNull(),
color: text("color").notNull(),
variantName: text("variantName").notNull(),
productID: integer("productID").references(() => products.id),
});

export const productVariantRelations = relations(productVariant, ({ one }) => ({
product: one(products, {
fields: [productVariant.productID],
references: [products.id],
}),
}));

export const productRelations = relations(products, ({ many }) => ({
productVariants: many(productVariant),
}));

export type Products = InferSelectModel<typeof users>;
export type ProductVariants = InferSelectModel<typeof productVariant>;
export type User = InferSelectModel<typeof users>;
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