sqlpostgresqlpsqlltree

Postgres ltree with variables in query


I use variable in psql like: \set user_id 10;

When i want use this variable in query like select * from users where user_id = :user_id; - it's ok

But when i use this for ltree column i have a problem select * from accounts where customer_id <@ :user_id?

Can you help me?


Solution

  • You need a string literal, so you have to quote the value:

    select * from accounts where customer_id <@ :'user_id';
    

    That will become

    select * from accounts where customer_id <@ '10';