drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.85k stars 593 forks source link

[BUG]: Relations fails with multiple models #736

Closed jmforj closed 1 year ago

jmforj commented 1 year ago

What version of drizzle-orm are you using?

0.26.5

What version of drizzle-kit are you using?

0.18.1

Describe the Bug

Whenever I try to fetch a model with multiple relations

const result = await db.query.users.findMay({
  with: {
    posts: true,
    project: true
  }
})

I get the following log:

Error fetching project PostgresError: could not identify an equality operator for type json
     at ErrorResponse (/Users/jmforj/Dev/forj/dapp-whitelabel/node_modules/postgres/cjs/src/connection.js:771:26)
     at handle (/Users/jmforj/Dev/forj/dapp-whitelabel/node_modules/postgres/cjs/src/connection.js:473:6)
     at Socket.data (/Users/jmforj/Dev/forj/dapp-whitelabel/node_modules/postgres/cjs/src/connection.js:314:9)
     at Socket.emit (node:events:513:28)
     at Socket.emit (node:domain:489:12)
     at addChunk (node:internal/streams/readable:324:12)
     at readableAddChunk (node:internal/streams/readable:297:9)
     at Readable.push (node:internal/streams/readable:234:10)
     at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

No issue if I only try with one relation.

Expected behavior

The result contains both relations

const result = {
  posts: [],
  project: {}
}

Environment & setup

Node 18.15.0, Typescript 5.1.3 & Postgres 3.3.5

jmforj commented 1 year ago

FYI I replaced postgres with node-postgres and I'm still facing the same issue:

error: could not identify an equality operator for type json

could it be related to my models?

EthanHogan commented 1 year ago

@jmforj It looks like you may have a syntax issue on line const result = await db.query.users.findMay({;

It looks like you are doing .findMay when it should be .findMany.

Hope that helps!

jmforj commented 1 year ago

thanks @EthanHogan you are right! haha although this was not the issue

It wasn't a drizzle bug but an issue with a column type in my database.

Thank for your support, closing this one.

minjie0501 commented 1 year ago

@jmforj could you elaborate on what was wrong with your column type in your db? I'm getting the same error when i'm trying to add multiple relations in my query but i can't seem to find out what would be wrong with my db schema

EDIT: changing my json fields to jsonb solved the error ✅