Drizzle Team

DT

Drizzle Team

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

Join

exclude models from bundle

Hello! I have a monorepo (turborepo + pnpm workspaces) with packages database, schemas & contracts and apps web(nextjs 13 app) & api(nestjs app). Database package hold all the tables, enums & relations definitions using drizzle-orm. Schemas is using the drizzle-zod to create needed body/query schemas. Contracts are ts-rest contracts shared between FE and BE....
No description

between operator

Hello i have a table where i have 2 columns where i save timestamps (epoch time) as integer. when i try to query it with the between operator i get no response. .where(between(1697013797353, table.startDate, table.endDate))...

Joining multiple instances of same table?

Suppose I have an items table where each item can have up to 3 tags. This is hardcoded - the items table has columns tag1id tag2id and tag3id. I want to find all items that have a given tag. The code below seems to select the proper items but I do not get the tag2 or tag3 properties on the result. Is there something I'm doing wrong, and more generally, is there a better way to achieve this? ```js let t1 = alias(tags, 't1'); let t2 = alias(tags, 't2');...

Cannot join tables where the columns name are the same.

If you do then the join table will overwrite the from table. it is really frustrating

Is the blob type supported for MySQL?

Hi, I'm wondering if the blob type for MySQL is currently supported in drizzle? If not, I was wondering if there are plans to support or if there are workarounds. Thanks.

Pg: Inserting new data with auto increment column

I am getting error while inserting data to postgres from nextjs api. Error: detail: 'Key (id)=(21) already exists.', I have table with three columns and the API is submitting values as name and status. I am guessing postgres would take care of auto increment. what is the issue here ? ``` id: serial('id').primaryKey(), name: varchar('name', { length: 256 }).notNull(),...

Is ilike vulnerable to SQL injections?

Modified from the post: https://orm.drizzle.team/docs/operators#ilike See code below: ```ts import { ilike } from "drizzle-orm";...

Optional fk

Hello, I have an optional fk and I'd like to db.update it to be null (point to nothing). I'm receiving an error while trying to set it to undefined: No values to set . What's the intended way to do it?...

Psql Array of text and `createSelectSchema`, `createInsertSchema`

It seems that drizzle-zod - createInsertSchema and createSelectSchema cannot infer correctly an array of text. My column; ``` colName: text('col_name').array()...

Migration struggled at some snapshot (existed table), Can I skip it?

As my understanding, Drizzle generate each database changes as snapshot. In my case migrate function was struggled at some snapshot with error like TABLE EXISTS cause I manually create/modify that table myself or it was an existed table before I'm using Drizzle. Now any new schemas can't migrate to database due to it's struggled at that snapshot. Can I skip it or anyway to solve this?
No description

`db:push` with `customType` causes data loss

Running db:push consecutively without changing the schema causes alterations and truncation, only for customType columns: You're about to change dollars column type from numeric(16, 2) to numeric(16,2) with 50 items...

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 ```...

update incrementing value (postgres)

So, i have a table: ```javascript export const VisitorCount = pgTable('visitor-count', { id: bigserial('id', { mode: 'number' }).primaryKey(), experimentId: bigint('experiment_id', { mode: 'number' }),...

Null fields not working as expected

Hi all, I am using Drizzle ORM with Zod validation for useForm, and I am running into issues with fields that are allowed to be undefined / null, but I get Zod errors stating that the value of the optional fields (when left empty) are invalid_type and in required. I was able to fix this by refining the field as follows: ...

Type Generated from $inferInsert does not change after column type update

I have a table called identities in a schema called identity. When I changed the phone_country_code column type from text to numeric, the sql generated from drizzle-kit was correct, but the type generated from inferInsert still has the old string type. (see attached image). ```typescript // Table definition phoneCountryCode: numeric('phone_country_code').notNull(), ...
No description

Is it possible to use INSERT statements inside a CTE (WITH query) +++ another question?

In PostgreSQL, you're allowed to use other DML statements (e.g., insert, update, delete) inside a CTE. Something like: ```sql WITH moved_rows AS ( DELETE FROM products...

SQL_PARSE_ERROR with Turso + Drizzle

getting
SQL string could not be parsed: near MINUS, "None"
SQL string could not be parsed: near MINUS, "None"
when running a push in development with this schema: ```js import { sqliteTable, text, numeric, uniqueIndex, integer } from 'drizzle-orm/sqlite-core'; import { sql } from 'drizzle-orm';...

onConflictDoNothing is not returning values

Hey, Given the following query: ```javascript...

push:sqlite cannot find module 'better-sqlite3'

im trying to hook up turso wind drizzle, and encounter thi error,
No description

How do you use 128-bit values with varbinary and what is the fastest way to search for rows?

I have been researching the fastest way to insert data into SQL tables, & I'm having a hard time using Drizzle with PlanetScale because PS doesn't support foreign keys; which means that PS is auto-sharded & the autoincrement primary keys are not going to be the same. You don't want to use UUID because 1.) it exposes your MAC address & 2.) the values don't always increase, i.e. non-monotonic. The current industry choice is ULID, which uses a 128-bit pattern where you take the JS new Date().getTime() & shift the lower 48-bits into the MSB & the lower 80 bits are a cryptographically-secure random number. (CSRN) generated at runtime. I don't like the idea of having to pay to generate random numbers at runtime so I created an npm package called linearid (LID) that uses a 128-bit value where the LSB is a 64-bit CSRN created upon server initialization, & the MSB is a 42-bit millisecond timestamp followed by a 22-bit spin ticker that gets reset every millisecond & incremented each time you call LIDNext(). If you call LIDNext() more than 2^22 (4,194,304) times in one millisecond it will spin wait until the next millisecond. This gives you a 128-bit monotonically increasing unique ID that you can use to look up data in SQL tables. Then to search for the SQL row, you extract the seconds timestamp from the LID, & search for the row by date range using the filesystems timestamps so you don't have to do a full table scan. Instead you search through all of the SQL rows created starting at the millisecond the LID was generated to something like three seconds after, which is much faster for searching for iNodes on disk....
No description