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
Following up @moderator
I also don't get it
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.I use relations without foreign keys
Alright... then I understand even less 👍
no foreign keys, just relations
@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
I started playing around with this
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.
@rubberduckies @A Dapper Raccoon @ohdontmindmeatall
That's what I thought as well.
well, i like doing it as i do, to each its own 🙂
If performance isn't bad, why not?
I appreciate the insights :)
Thank you!