Drizzle Team

DT

Drizzle Team

The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!

Join

How to type results that includes relations ?

I have two tables: collections and products. Collection has many products. ```ts const results = await db.query.collections.findMany({ with: {...

Error: Either `connectionString` or `host, port, etc.` params be provided in config file

Hey, So i'm trying to setup Drizzle with PlanetScale, and want to use the drizzle kit push command, but i run into this error: ```console...

Setting AUTO_INCREMENT starting value

In MySQL it is possible to make auto incremented column to start from specified value. For example ALTER TABLE tbl AUTO_INCREMENT = 100; Is it possible to do that using Drizzle?

MySQL varchar gets inferred as MySqlText?

next-auth adapter help again. Is this my bug or Drizzle's? (Probably Drizzle's because Drizzle STINKS) TL;DR: MySQL varchars are getting inferred as MySqlText. Part of the requirements of next-auth is to provide a minimum schema but also let folks extend off of it. So, I have a minimum schema built for MySQL and am inferring it's type. But it looks to me like the inferred type of MySQL's varchar becomes MySqlText when it gets inferred. The problem with this ends up being that, when folks want to provide their custom table with the varchar (which is correct) they get an error because TS thinks it's supposed to be a MySqlText....

Problem with infering json type in zod schema

When using the following table/schema: ```ts const Test = mysqlTable( "Test", {...
Solution:
The .$type() helper only works on the type level, it cannot change the runtime behavior. The schema validation happens at runtime, thus it doesn't know about your type. If you want to validate your json field according to your type, you would need to refine the insert schema, like this: ```ts const TestInsertSchema = createInsertSchema, { value: () => z.object({ a: z.string(), b: z.number() }), });...

can't execute basic pg cmnds in drizzle orm .

I have written my schema and generated the table. I can use pg client to interact with the db but not with drizzle orm for some reason i am executing a simple command
const result = await db.select().from('auth_key')
const result = await db.select().from('auth_key')
it gives an error stating that
e: ReferenceError: auth_key is not defined
e: ReferenceError: auth_key is not defined
...

Implement full text search in postgres

Hi. I stumbled upon this issue (https://github.com/drizzle-team/drizzle-orm/issues/247) which was very useful. I then wanted to add support for weights. I was able to achieve 99% of the task but the last 1% is where I'm stuck and need help. Generated sql: ```sql ALTER TABLE "table_name" ADD COLUMN "vec" "tsvector GENERATED ALWAYS AS (setweight(to_tsvector('english', coalesce(title, '')), 'A') || setweight(to_tsvector('english', coalesce(description, '')), 'B')) STORED";...

Select One UX improvement

I have this
const arr = await db.select().from(worlds).where(eq(worlds.id, id)).limit(1)
const world = arr[0]
const arr = await db.select().from(worlds).where(eq(worlds.id, id)).limit(1)
const world = arr[0]
...

Trouble getting drizzle to run on server side in nextjs

Hi, having some trouble getting drizzle db connection to run on server side as documentation shows. i followed the steps multiple times and each time only leads to working if the environment variables are prefixed with NEXT_PUBLIC or hardcoded (node env variables not loading) and would have to be exposed to browser which i dont want to do. is there a reason its only running on client side calls? i have tried moving the files to different areas in my next project, including the server folder but...

Auto update timestamp fields

How to auto update fields like updated_at?

Relational query erroring on workers + D1

Only able to get relation queries to work while running the worker in local mode. While running remote (--remote) or deployed it errors ``` [ERROR] Uncaught (in promise) Error: D1_ERROR...

Infer return type for relational query with pagination

I have created a function that can paginate data for relational queries, problem is i cant figure out how to make to include the relations that are passed into the inferred return type ``` async paginatedQuery< ReturnType extends drizzleOrm.InferModel<Table>,...

How to implement triggers or db hooks ?

Is there a way to know if sql statement got executed successfully or not ? I wanted to run a code when row gets inserted or updated ? can custom loggers be used to implement the same ?...

noob help: INSERT with JOIN SQL to Drizzle

Frontend developer dabbling with SQL for the first time in years. With the help of ChatGPT I have the following query which works as intended. How do I construct with Drizzle such that I can supply my values using the .values() method? ``` INSERT INTO billing (team_id, credit_change, credits, type) SELECT teams.id, billing.credit_change, teams.credits + billing.credit_change, 'runtime'...

OrderBy with dynamic queries dont work

....orderBy(sql`${products.xyz} ${order}`)
....orderBy(sql`${products.xyz} ${order}`)
```...

Is there a way to limit update to just 1 document?

I know there will be multiple documents matching this where query, but I only want to update 1 of them at a time.
```export const joinTable = async (tableId: number, seatedPlayerId: number) => { await db .update(tableSeats) .set({ seatedPlayerId })...

How to get InferModel to work with Relation model?

Is there a way to get the infer type for a relation model? eg: type User = InferModel<typeof users, "select">; type UserWithPosts = InferModel<typeof users, { posts: true }, "select" >;...

MySQL column type: "Generated"?

Does drizzle support defining generated columns? https://planetscale.com/courses/mysql-for-developers/schema/generated-columns Something like a columnType sql If not, is it dangerous to send MySQL a custom DDL statement to Add a generated column? ...

Performance questions

I'm using Drizzle ORM with Planetscale DatabaseJS driver and my application has spots where I want to decrease number of HTTP requests to my database. I have following questions: 1. Does Drizzle+DatabaseJS make separate HTTP request for each query? (I know I should be asking this question to DatabaseJS creators, but asking here in case someone is aware of that) 2. If it does, what would be the best way to group several queries so that they are performed in one HTTP request?...