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:

Internal error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(select json_array(`CategoryTable_parent`.`id`, `CategoryTable_parent`.`paren...' at line 1


Whole query:

Query: select `CategoryTable`.`name`, `CategoryTable_parent`.`data` as `parent` from `category` `CategoryTable` left join lateral (select json_array(`CategoryTable_parent`.`id`, `CategoryTable_parent`.`parent_id`, `CategoryTable_parent`.`slug`, `CategoryTable_parent`.`name`, `CategoryTable_parent`.`image`, `CategoryTable_parent`.`description`) as `data` from (select * from `category` `CategoryTable_parent` where `CategoryTable_parent`.`id` = `CategoryTable`.`parent_id` limit ?) `CategoryTable_parent`) `CategoryTable_parent` on true -- params: [1]


and the code used to query the data:

db.query.CategoryTable.findMany({
    with: {
      parent: true,
    },
  })


Any ideas if that is possible to work, or am I doing something wrong here?
15_21_37_801.png
Was this page helpful?