Drizzle Team

DT

Drizzle Team

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

Join

Help migrating a nested Prisma update

Been trying to set this up without going through the raw sql route for some time but cant seem to wrap my head around it. Basically the structure here is I have a Result that can optionally have an alert or a note. If an alert or note already exist then update them. Any help here is appreciated it! thank you ``` const updatedResult: Result = await prisma.result .update({ where: {...

Which typescript type should I use for select?

Hi, this question might be confusing. Let me explain the problem, I'm building a common function for drizzle().select() somethine like ``` const data = await selectQuery(ctx, tableName, {id:tableName.id, data:tableName.data}); // should return {id:number,data:string} // select...

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

Using a placeholder with an inArray

I'm porting a query to a prepared statement, in the query I used to do something like:
.where(inArray(users.id), ['abc', 'def']))
.where(inArray(users.id), ['abc', 'def']))
...

DB insertion - Date field (mode: string) is using default value instead of passed value

Trying to insert an Order object which has a "pickupTime" Date field to db, but upon insertion the created item uses the model default value instead of the passed date. This is the insertion : ```js const [o] = await db.insert(orders).values(newOrder).returning()...
No description

Best practice on connections for PostgreSQL

I got the following error; remaining connection slots are reserved for non-replication superuser connections ...

Minimum value for Postgres bigserial

I want my id tables to use bigserial, but i want the first row to be inserted with the value 222222. Is there a way to do this? id: bigserial('id', { mode: 'number', ...}).primaryKey()...

Prepend raw fields with table/subquery name in rendered SQL

Currently, it doesn't seem like raw fields in a select statement hold any information on the table/subquery they're used in. This results in subsequent usage of these fields elsewhere rendering to SQL as "field_name" instead of "alias"."field_name". This actually causes issues when you have overlapping field names in sql in your joins. Example:...

Is there a faster way todo this?

```typescript await db.transaction(async (tx) => { for (let i = 0; i < data.length; i++) { tx.update(cities) .set({ name: data[i].name })...

Using one function to query many tables of same structure

Hello i have many tables (5-6) that have same structure like this ```ts export const speciality = pgTable('speciality', { id: serial('id').primaryKey(), name: varchar('name', { length: 255 }).notNull()...

How can I use a schema to generate something like this:

We have a 1 to many relationship between items and tokens, but an item can only have one minted token, example below: CREATE TABLE Token ( id INT PRIMARY KEY AUTO_INCREMENT, itemId INT,...

pgTable - raw sql

Hi is it possible to add in raw sql after pgTable ? I have them in kysely migrations, but would like to use drizzle schema. I want to add some check constraints, triggers, rls. I know I can do them in migrations, but wanted it in the same file....

HELP: Key columns "resource_id" and "id" are of incompatible types: text and integer.

I don't know what to do anymore i've tried all the possible solutions i could think about, `export const bookmarks = pgTable("bookmarks", { id: serial("id").primaryKey(), user_id: text("user_id")...

Docs unclear on connection pool vs. direct connection

In the docs under "Supabase" it teaches you how to setup Drizzle w/ supabase. The last part shows how to set up the connection: ```ts import { drizzle } from 'drizzle-orm/postgres-js' import postgres from 'postgres' import { users } from './schema'...

Serializable isolation level - for update selects

Hi, Is there a way to add "for update" to the end of the query to help with serializable transactions? Without it, we cannot read then later update the same record in a transaction in postgres. So far, I've just written out the sql myself and lose most of the types....

Json object aggregate

In a multilingual app, I am using relational queries (but I could also just work with normal selects) to retrieve rows and their related translation strings. My schema has a bunch of tables like so: ```ts export const projects = pgTable('projects', { id: nanoid('id').default(generateNanoid()).primaryKey(), createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),...

many to many self reference

I'm trying to define "follows" & "followers" relations on a user entity. Obviously this is a many to many relation, but how to I define both "edges" in my relation? ```typescript const userToUsers = table("users", { userId: text(),...

Extract a type from a table

Imagine we have a user table like ```ts export const user = pgTable( "user", {...

Best practice on running migrations

I have a serverless framework setup and during my testing I've been using the db push but now that I have something setup to actually test my migrations I'd like to run:
await migrate(db, { migrationsFolder: "drizzle" });
await migrate(db, { migrationsFolder: "drizzle" });
whats the best path to actually running this from an npm script or from a ci for example. Or even locally without using push. Basically, I've generated my sql migrations but I now need to run them....

Initial query after Neon's suspend_compute

Experiencing problem for the first query when Neon is starting up