how to do one to many?
hello everyone i am having some problems doing a one to many relation and select on a post schema i have. for context i get this error when ever i try to make a query using the where and with here is my select statement and here is my schema:
5 Replies
any help would be very helpful thank you!
Hi there. Your schema, or at least from what I can see of it, looks good. Is there more to the error message, like a stack trace?
let me take a look!
here is the full error message: ⨯ Internal error: TypeError: Cannot read properties of undefined (reading 'referencedTable')
at normalizeRelation (./node_modules/drizzle-orm/relations.js:211:118)
at PgDialect.buildRelationalQueryWithoutPK (./node_modules/drizzle-orm/pg-core/dialect.js:977:101)
at QueryPromise._getQuery (./node_modules/drizzle-orm/pg-core/query-builders/query.js:91:25)
at QueryPromise._toSQL (./node_modules/drizzle-orm/pg-core/query-builders/query.js:106:24)
at eval (./node_modules/drizzle-orm/pg-core/query-builders/query.js:69:42)
at Object.startActiveSpan (./node_modules/drizzle-orm/tracing.js:14:14)
at QueryPromise._prepare (./node_modules/drizzle-orm/pg-core/query-builders/query.js:68:60)
at eval (./node_modules/drizzle-orm/pg-core/query-builders/query.js:115:19)
at Object.startActiveSpan (./node_modules/drizzle-orm/tracing.js:14:14)
at QueryPromise.execute (./node_modules/drizzle-orm/pg-core/query-builders/query.js:114:60)
at QueryPromise.then (./node_modules/drizzle-orm/query-promise.js:26:17)
digest: "88883804"
⨯ Internal error: TypeError: Cannot read properties of undefined (reading 'referencedTable')
at normalizeRelation (./node_modules/drizzle-orm/relations.js:211:118)
at PgDialect.buildRelationalQueryWithoutPK (./node_modules/drizzle-orm/pg-core/dialect.js:977:101)
at QueryPromise._getQuery (./node_modules/drizzle-orm/pg-core/query-builders/query.js:91:25)
at QueryPromise._toSQL (./node_modules/drizzle-orm/pg-core/query-builders/query.js:106:24)
at eval (./node_modules/drizzle-orm/pg-core/query-builders/query.js:69:42)
at Object.startActiveSpan (./node_modules/drizzle-orm/tracing.js:14:14)
at QueryPromise._prepare (./node_modules/drizzle-orm/pg-core/query-builders/query.js:68:60)
at eval (./node_modules/drizzle-orm/pg-core/query-builders/query.js:115:19)
at Object.startActiveSpan (./node_modules/drizzle-orm/tracing.js:14:14)
at QueryPromise.execute (./node_modules/drizzle-orm/pg-core/query-builders/query.js:114:60)
at QueryPromise.then (./node_modules/drizzle-orm/query-promise.js:26:17)
digest: "88883804"
GET /dashboard/editor/23423 500 in 16531ms
is it because i have to add an refernece to my post schema perhaps?
im actually unsure how to do this the doc's are very bad at explaining in step to step how to set up one to many
okay so i found a solution: const post = await database
.select()
.from(posts)
.where(eq(posts.id, params.postId))
.leftJoin(tags, eq(tags.postId, params.postId))
.leftJoin(links, eq(links.postId, params.postId));
i had to do a leftJoin but that kinda defeat the purpose
Hmm, it might be that there's something wrong with a relation that I can't see in the code you provided. A way I verify that my relations are valid is by running Drizzle Studio, it will error if there's an invalid relation in your schema
yeah but my drizzle studio seems to work fine