Not sure why we need to make relations when we have foreign keys?

After reading the docs, I understand that relations are a way to enforce some level of referential integrity on the application level as opposed to the database level. What I am confused about is whether I need to use them in order to make sure typescript is happy/to use the query api. Some clarity would be great on this as my first instinct is to completely forego the relations and just use foreign keys.
14 Replies
Daniel Drozdov
Daniel Drozdov5mo ago
Following up @moderator
ohdontmindmeatall
I also don't get it
A Dapper Raccoon
I've been wondering about this as well, but still haven't explored it to any depth. My best understanding is that you use relations() to define "virtual fields" which reference other models, for use specifically inside Drizzle's db.query RQB interface (maybe?). It is also my understanding that relations() are not presently factored in to the Typescript types as you might expect - a db.query for a User containing a with: { posts: true } will not automatically have a return type which includes the posts property, but it will exist on the data. I believe I read that this is a planned feature though. So you need to use foreign keys either way, but relations() enables you to create a "virtual field" for the relation to query for and access the data more intuitively, wherein Drizzle handles building the appropriate query. You can do the same without it, but then you're responsible for factoring the foreign keys and joins and such into your query. But using relations() and Typescript, you currently also need to somehow cast or coerce the query results to a type including the relational fields to make full use of the feature. This is all just my current understanding - I also would love to hear a more canonical/informed answer.
rubberduckies
rubberduckies5mo ago
I use relations without foreign keys
A Dapper Raccoon
Alright... then I understand even less 👍
rubberduckies
rubberduckies5mo ago
no foreign keys, just relations
No description
rubberduckies
rubberduckies5mo ago
@A Dapper Raccoon, @Daniel Drozdov , I'm not sure if this is the real origin story for drizzle to include "relations"but certain cloud database providers, like Neon, which was all the rage a few months ago, don't (or at least didn't) allow for foreign keys in their postgres offer. Relations are a way to mimic that
Daniel Drozdov
Daniel Drozdov5mo ago
I started playing around with this
Daniel Drozdov
Daniel Drozdov5mo ago
I use foriegn keys because I think that is proper db design and maintains some level of referential integrity, not orm specific relations. Relations are really only necessary if you want to use things like with syntax and don't care to deal with aggregating data when its returned: https://orm.drizzle.team/docs/joins#aggregating-results Adding relations adds some initial overhead when writing the drizzle schema however it does simplifying db calls and allows you to use with api and not worry about properly aggregating your data(and testing aggregation). TLDR: Use FK for referential integrity, use relations to take advantage of some the orm features like with and to avoid having to manage data aggregation. If you just want to use drizzle like a query builder there is no need for relations
Drizzle ORM - Joins
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Daniel Drozdov
Daniel Drozdov5mo ago
@rubberduckies @A Dapper Raccoon @ohdontmindmeatall
ohdontmindmeatall
That's what I thought as well.
rubberduckies
rubberduckies4mo ago
well, i like doing it as i do, to each its own 🙂
ohdontmindmeatall
If performance isn't bad, why not?
A Dapper Raccoon
I appreciate the insights :) Thank you!
Want results from more Discord servers?
Add your server