Self referencing table query
So I have a
category
table that references itself, each category can have a parent that is also a category.
All seems to be working fine, migration is being generated and works perfectly fine.
But then when I try to query the data with references it fails on sql error:
Whole query:
and the code used to query the data:
Any ideas if that is possible to work, or am I doing something wrong here?31 Replies
this is an example with postgres: https://drizzle.run/ps9ydkhwlxzg9h7xehkgayp1
Drizzle Run
Self one-to-many relations - Drizzle Run
It seems that you are using MariaDB?
If so, sorry, it is not yet supported 😦 https://github.com/drizzle-team/drizzle-orm/pull/1692
GitHub
Add support for Maria DB (core API implementation) by L-Mario564 · ...
(Partially) Addresses #203.
This PR aims to add support for Maria DB, which simply extends the existent MySQL dialect.
Tasks:
Implement mariadb driver.
Write core API integration tests for the m...
ahh that makes sense, seems I'll just migrate to postgres as seems there's many more tutorials for it, and at that point it doesn't make any difference for me
thanks for the help!
Postgres, the King :p
SQLite is fine too
https://turso.tech/ has made excellent contributions with PGLite (that enable SQLite for Drizzle Run)
Turso
Turso — SQLite for Production
Turso is SQLite for production: great developer experience and efficiency with essential quality of life improvements for modern applications.
hey @Raphaël M (@rphlmr) ⚡ one more question, is there any difference in using ". references" vs using the extraConfig?
No for simple case. Extra config is more powerful (can make compound keys)
Perfect, thank you!
hey @Raphaël M (@rphlmr) ⚡ me again 😅
so as I mentiond at the beginning, what I was tring to achieve was to have CATEGORIES table for a samll shop, where each category can have 1 parent or null (top category)
then what I would need is to have a way to query all of them in a tree like structure, top to bottom, which seems to be done in sql with "recursive" but that's still not yet available in drizzle
and if I have:
I can query everything using
with
but then it goes bottom to top, so the other way around
is there a way to solve that in drizzle at the moment?
my second approach after your help was to do it the other way, to have a separate column and many
relation, but that's more complicated:
that can be queried with:
not sure if that's a drizzle only question but maybe you'll be able to point me to a proper solution with drizzledrizzle recursive pr: https://github.com/drizzle-team/drizzle-orm/pull/1405
GitHub
Feat: select without from and with recursive by Angelelz · Pull Req...
This will close #372 and will close #209. Also related to #1215?
This PR depends on #1218.
Upon merging, It will be possible to write a select statement without the .from() method. That will make p...
Hum I tried something but without this PR it is not that clean
well again thank you! although that is such a madness that I'll just stick to the nested with solution posted above, as at least I understand what's going on 🤣
will try to migrate when recursive lands
although typing the result of these is a nightmare for me 🤨
At this point I would try a Awaited<ReturnType<typeof theQueryConst>> but I wonder if TS will survive 😅
well for now I did this manually 🤣
works ok'ish
Recursion is one of the few things I can’t get into 😅
It crashed my brain. This and generator functions 😆
well TS is great, but I have no idea how people do come up with all these typings, that's madness, then recursion on itself is easy 🤣
Yeah sometimes I create types helper and later I can’t even guess why and how I did that.
Drizzle type system is 🔥 (even if it has some bugs), huge respect to the team 😮
well hopefully one day I'll uinderstand some of these typings 😅
other than that, you're a ⭐ thank you!
hey @Raphaël M (@rphlmr) ⚡ just FYI, thanks to your example and some googling I just understood how recursive query works, and even been able to write my own that takes current category and lists all its the the parents recursively 😅
Wow nice! I am happy to read that it helped you, I built Drizzle Run to achieve this goal 🥳
@Raphaël M (@rphlmr) ⚡ if you look at the console log of this, it's actually snake casing the keys, besides manually camel-casing each key, is there a way to fix this?
and I think same issue, but it's also not running my custom column type processing
For the snake_case it's
.as("parent_id")
==> .as("parentId")
if I add another column to the schema and try to reference it, it comes down in snake case https://drizzle.run/v7feo604avcvkfr5jz65km3u
Drizzle Run
(fork) Raw recursive query - Drizzle Run
Ah yes. It’s because we run a raw query, so we lose Drizzle’s auto mapping :/
dang, and I'm assuming there's no easy workarounds?
give me 15 min
the alternative way (manual)
The only query that needs that is the referent query (left side of the unionAll)
@Raphaël M (@rphlmr) ⚡ Wow, this is great and very helpful for the camelCase issue!
Unfortunately, there's just too much I'm losing with the "raw" to work around beyond just the camelCase issues. I've got custom types not running, I've got a
bigserial
+bigint
columns coming in as strings, etc. Is there a way to manually convert each record using a table's columns definition? That would be a great stopgap solution until $withRecursive comes out
I think I'll just do a manual with recursive query with sql
to get the ids, then use the query api to get the actual items. I think my size will permit it. Then I'll swap out with a single recursive query when it's ready