Search code examples
databasepostgresqltreeelixirecto

Selecting direct children with Postgres ltree using Elixir


I'm trying to select any children that are a single level below with ltree.

For example, if I had Car.Ford, the query would grab any child with a path such as Car.Ford.Fiesta, Car.Ford.Fusion, Car.Ford.Mustang.

How can I build this query using ltree, if possible, specifically using Elixir?

Right now I'm using

from c in query, where: fragment("path <@ ?", c.path)

But it returns all entries with the path in it.


Solution

  • Figured this out.

    The documentation on Postgres states that the {} in an lquery limits the number of labels it will match, the documentation from the developers clarifies that this is actually to limit the number of levels to search.

    'My.Example.*{1}'
    

    That will match anything one level below a path starting with My.Example