简体   繁体   中英

Syntax error in query using ltree on Postgresql 9.6.5

I have a problem with ltree extension on Postgresql 9.6.5

I have a table called category with the following DDL (I simplified it a bit):

CREATE TABLE dictionary.category
(
    id serial not null constraint category_pkey primary key,
    name text not null,
    parent_id integer constraint category_parent_id_fkey references dictionary.category
);

After creation of ltree extension:

CREATE EXTENSION ltree;

I'm trying to make some query eg:

SELECT id, text2ltree(name) FROM dictionary.category;

or

SELECT id, name::ltree FROM dictionary.category;

or escaping column name

SELECT id, text2ltree("name") FROM dictionary.category;

And it gives me:

ERROR:  syntax error at position 12

all the time

But when I try:

SELECT id, text2ltree('a.b.v') FROM dictionary.category;

or

SELECT id, text2ltree(id::text) FROM dictionary.category

it gives me correct results.

I suppose it is related to the fact that name is a reserved keyword. But why escaping do not work? Also I trie to rename a column to something like abcd it gives me syntax error anyway.

Thanks everyone in advance!

Answering my own question. It appears that those error message is not related to a query itself. It is related to a text that can by contained in ltree path. It appears that l tree path allows only alfanumeric characters and nothing more.

SELECT id, text2ltree(regexp_replace(name, '[^[:alpha:]]', '', 'g')) FROM dictionary.category;

returns correct results.

Anyway, the error message is very misleading.

We had the same problem. We use extract(epoch FROM date_start) to generate a LTREE from a date column, but one of our records returns a negative number.

PostgreSQL Date/Time Functions and Operators
epoch
For timestamp with time zone values, the number of seconds since 1970-01-01 00:00:00 UTC (can be negative); for date and timestamp values, the number of seconds since 1970-01-01 00:00:00 local time; for interval values, the total number of seconds in the interval

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM