How to Chain multiple selects
I am new to ORMS and SQL so please bear with me. I have the attached schema for the chat component of the app that I am building. I am a bit lost how I would chain this sequence of select statements
help much appreciated
19 Replies
here is my query
i am getting this error
Did you define the relations in drizzle orm
so its not like Prisma and infer the relations @mast1999 ? would i do this in a seperate file? because what happens when i have to regenerate the schema file through introspection? I am mainly using drizzle for introspection and not for migrations
Yes, currently in drizzle you have to define the relations between the tables yourself if you want to use the relational queries. Introspection doesn't generate them.
The other option is to use joins to get the same result but it's more work.
https://orm.drizzle.team/docs/joins
Joins [SQL] – DrizzleORM
Drizzle ORM | %s
i decided to use joins because i couldnt figure out how to setup the relationships post introspection. i wish the documentation was a bit more clear.
It can be kind of confusing 😄
Which part is hard to understand? Maybe I can give you a hand?
this is my schema for the chat portion of my app
i ended up with a left join
@mast1999
i tried all kinds of stuff to get db.query work
finally threw in the towel
It's kind of hard to explain here, want to jump on a call?
But in short each table need a relation and in that relation you should define what kind of relation that table has to the other tables.
For example:
The Fields are the columns on the table that you are defining the relation for and the references is well the columns on the table it is referencing 😄
This for example is a one to many relationship.
many to many relationships are more complicated because you have to define a middle table that stores a reference from each table in there.
And then define a one to one relationship in the relations of the main tables.
Similar to the docs: https://orm.drizzle.team/docs/rqb#many-to-many
Relational queries – DrizzleORM
Drizzle ORM | %s
BTW after defining the you need to pass the
schema
s to the drizzle instance when you are creating it.
https://orm.drizzle.team/docs/rqb#queryingRelational queries – DrizzleORM
Drizzle ORM | %s
i can jump on a call. are you free now?
Yep
I'm free now
i dont know know how to start a call 🙂
new to discord
K
i missed the one part
because the example only give many
hang on. let me figure out this call stuff
You can right click on my image and the it should say call as one of the options