SPS | Shootmail
SPS | Shootmail
Explore posts from servers
DTDrizzle Team
Created by SPS | Shootmail on 10/9/2023 in #help
Query depth issue Cloudflare D1
I am encountering Expression tree is too large (maximum depth 20) issue mostly with relation queries while using Cloudflare D1. As explained in this issue https://github.com/cloudflare/workers-sdk/issues/4094, this issue is because of the query depth limit set by them which they are considering increasing. My question is, if for now, I were to refactor this query, what would be the most performant way? 1. Using batch 2. Using multiple queries with Promise.all
let result = await d1.query.products.findFirst({
with: {
category: true,
mediaOnProducts: {
with: { media: true }
},
variants: {
with: {
items: true
}
},
variantCombinations: {
with: {
items: true,
media: true
}
}
},
where: (products, { eq }) => eq(products.id, productId)
});
let result = await d1.query.products.findFirst({
with: {
category: true,
mediaOnProducts: {
with: { media: true }
},
variants: {
with: {
items: true
}
},
variantCombinations: {
with: {
items: true,
media: true
}
}
},
where: (products, { eq }) => eq(products.id, productId)
});
1 replies
DTDrizzle Team
Created by SPS | Shootmail on 9/20/2023 in #help
push:sqlite creating weird tables
When I run push:sqlite, tables like __old_push_categories are created and are automatically refrenced from the other tables. Now when I do a normal insert, I get this error no such table: main.__old_push_categories
8 replies
DTDrizzle Team
Created by SPS | Shootmail on 7/1/2023 in #help
Modelling self relations
I have a table categories with a parent fields:
{
id: text("id").notNull().primaryKey(),
....
parent: text("parent").references((): AnySQLiteColumn => categories.id)
}
{
id: text("id").notNull().primaryKey(),
....
parent: text("parent").references((): AnySQLiteColumn => categories.id)
}
Here one category can have one parent category. But, one category can have many subcategories. I have defined my relations:
export const categoryProductsRelation = relations(categories, ({ many, one })=>({
subcategories: many(categories, { relationName: "subcategories" })
}));

export const parentCategoryRelation = relations(categories, ({ many, one })=>({
parent: one(categories, {
fields: [categories.parent],
references: [categories.id]
})
}));
export const categoryProductsRelation = relations(categories, ({ many, one })=>({
subcategories: many(categories, { relationName: "subcategories" })
}));

export const parentCategoryRelation = relations(categories, ({ many, one })=>({
parent: one(categories, {
fields: [categories.parent],
references: [categories.id]
})
}));
But I am getting this error:
There is not enough information to infer relation \"categories.subcategories\"
There is not enough information to infer relation \"categories.subcategories\"
10 replies
DTDrizzle Team
Created by SPS | Shootmail on 6/29/2023 in #help
Can't figure out how to design relational query
I have three tables - categories - products - media I want to retrieve products that belong to a particular category, along with the product category and all images. The results should be paginated and should be filtered by category_slug, organization_id. Relationships: 1. Many products can belong to one category (products - categories = Many-to-one) 2. Many products can have many images (products - images = Many-to-Many) I have tried:
const productsQuery = db.query.categories.findMany({
with: {
productsOnCategory: {
with: {
mediaOnProducts: {
with: { media: true }
}
},
where: (products, {and, eq}) => and(eq(products.isVisible, 1), eq(products.organizationId, orgId))
}
},
where: (categories, { and }) => and(eq(categories.slug, slug), eq(categories.isVisible, 1), eq(categories.organizationId, orgId))
});
const productsQuery = db.query.categories.findMany({
with: {
productsOnCategory: {
with: {
mediaOnProducts: {
with: { media: true }
}
},
where: (products, {and, eq}) => and(eq(products.isVisible, 1), eq(products.organizationId, orgId))
}
},
where: (categories, { and }) => and(eq(categories.slug, slug), eq(categories.isVisible, 1), eq(categories.organizationId, orgId))
});
Problem: Cannot paginate the products using limit and offset as offset is only available for the top level relation which here is categories.
db.query.products.findMany({
with: {
category: {

}
}
});
db.query.products.findMany({
with: {
category: {

}
}
});
Problem: Cannot filter on the basis of category_slug since
where
where
is not available inside nested category I guess because this is a Many-to-one relation. How can achieve the result I want using the relation queries?
9 replies
DTDrizzle Team
Created by SPS | Shootmail on 6/19/2023 in #help
Error while push:sqlite after update
12 replies
DTDrizzle Team
Created by SPS | Shootmail on 5/21/2023 in #help
Drizzle kit generate gives error after upgrade
I have updated drizzle-orm to 0.26.0 and drizzle-kit to 018.0. I have defined relations according to the docs, to use the relational queries. When I run drizzle-kit generate:sqlite, I am getting this error




19 replies
DTDrizzle Team
Created by SPS | Shootmail on 5/19/2023 in #help
Build queries dynamically with conditions.
How to build query on the basis of if-else conditions? I have tried doing it as below
let dbQuery = db.select().from(orders).where(and(eq(orders.organizationId, orgId), ne(orders.orderStatus , "CART"), eq(orders.id, Number(query))));
let dbQuery = db.select().from(orders).where(and(eq(orders.organizationId, orgId), ne(orders.orderStatus , "CART"), eq(orders.id, Number(query))));
if( status ){
if(status == "COMPLETE")
dbQuery.where(eq(orders.orderStatus, "COMPLETE"));
else if(status == "PROCESSING")
dbQuery.where(eq(orders.orderStatus, "PROCESSING"));
else if(status == "CANCELLED")
dbQuery.where(eq(orders.orderStatus, "CANCELLED"));
}
if( status ){
if(status == "COMPLETE")
dbQuery.where(eq(orders.orderStatus, "COMPLETE"));
else if(status == "PROCESSING")
dbQuery.where(eq(orders.orderStatus, "PROCESSING"));
else if(status == "CANCELLED")
dbQuery.where(eq(orders.orderStatus, "CANCELLED"));
}
But the where conditions don't seem to add and the result is returned without them.
3 replies
DTDrizzle Team
Created by SPS | Shootmail on 4/22/2023 in #help
Get column name in error messages
3 replies
DTDrizzle Team
Created by SPS | Shootmail on 4/10/2023 in #help
Query for Prisma like insert in multiple joined tables
Is there a way to achieve insert in multiple tables joined by foreign keys in one query like Prisma?
{
orderStatus: OrderStatus.CART,
organization: {
connect: {
id: orgId
}
},
products: {
create: {
price: price,
product: {
connect: {
id: product.id
}
},
quantity: orderProduct.quantity,
taxPercent: taxPercent,
variants: {
createMany: {
data: variantsOnOrder
}
}
}
}
}
{
orderStatus: OrderStatus.CART,
organization: {
connect: {
id: orgId
}
},
products: {
create: {
price: price,
product: {
connect: {
id: product.id
}
},
quantity: orderProduct.quantity,
taxPercent: taxPercent,
variants: {
createMany: {
data: variantsOnOrder
}
}
}
}
}
Or subsequent inserts in a transaction is the way?
5 replies
DTDrizzle Team
Created by SPS | Shootmail on 3/28/2023 in #help
Migrating from Prisma
Hi, I have an existing project already in production built using prisma and postgres (supabase). I am planning to go serverless and hence planning to migrate to drizzle + PlanetScale. Here is how I am planning it: 1. Use Prisma to generate the entire schema structure in MySQL (Planetscale) 2. Use Drizzlekit to introspect and generate types. 3. Use Drizzle ORM for querying. Am I thinking in the right direction? Or is there a recommended way?
2 replies
DTDrizzle Team
Created by SPS | Shootmail on 3/20/2023 in #help
require() of ES Module is not supported planetscale serverless + sveltekit
Hi, I am getting this error when I user planetscale serverless with drizzle orm
require() of ES Module D:\svelte-edge\node_modules\@planetscale\database\dist\index.js from D:\svelte-edge\node_modules\drizzle-orm\planetscale-serverless\session.js not supported.
Instead change the require of index.js in D:\svelte-edge\node_modules\drizzle-orm\planetscale-serverless\session.js to a dynamic import() which is available in all CommonJS modules.
require() of ES Module D:\svelte-edge\node_modules\@planetscale\database\dist\index.js from D:\svelte-edge\node_modules\drizzle-orm\planetscale-serverless\session.js not supported.
Instead change the require of index.js in D:\svelte-edge\node_modules\drizzle-orm\planetscale-serverless\session.js to a dynamic import() which is available in all CommonJS modules.
6 replies