首页 > 解决方案 > postgres:不能在名为“文本”的列上使用 to_tsvector

问题描述

我正在使用 postgres 11.5 并尝试执行以下操作:

update ccnc set fulltext_tokens = to_tsvector(title || '. ' || description || '. ' || text ) where fulltext_tokens is NULL;

这导致

ERROR:  column " text" does not exist
LINE 1: ...o_tsvector(title || '. ' || description || '. ' || text ) wh...
                                                             ^
HINT:  Perhaps you meant to reference the column "ccnc.text".

但是,使用 ccnc.text 也没有帮助:

felix=# update ccnc set fulltext_tokens = to_tsvector(title || '. ' || description || '. ' || ccnc.text) where fulltext_tokens is NULL;
ERROR:  missing FROM-clause entry for table " ccnc"
LINE 1: ...o_tsvector(title || '. ' || description || '. ' || ccnc.text...

...相应的列也没有什么奇怪的(没有尾随空格等):

felix=# \d+ ccnc
                                                               Table "public.ccnc"
     Column      |            Type             | Collation | Nullable |             Default              | Storage  | Stats target | Description
-----------------+-----------------------------+-----------+----------+----------------------------------+----------+--------------+-------------
 id              | integer                     |           | not null | nextval('ccnc_id_seq'::regclass) | plain    |              |
 description     | text                        |           |          | ''::text                         | extended |              |
 text            | text                        |           |          |                                  | extended |              |
 title           | text                        |           |          |                                  | extended |              |

编辑:引用也无济于事,例如:

update ccnc set fulltext =  title || '. ' || description || '. ' || "text";
ERROR:  syntax error at or near ""text""
LINE 1: ... fulltext =  title || '. ' || description || '. ' || "text";

对于如何创建名为 fulltext_tokens 的新列,我将不胜感激。先感谢您 :-)

标签: postgresql

解决方案


尽管 Richard 提出了非常有价值的评论,即首先不使用诸如“text”之类的列名,但避免错误的一种方法是使用concatorconcat_ws代替 concat 运算符||

例如:

update ccnc set fulltext = concat_ws('. ', title, description, text)

推荐阅读