魔术专栏“名称”从何而来?
-
16-10-2019 - |
题
我偶然得到了这个:
db=> select name from site;
ERROR: column "name" does not exist
LINE 1: select name from site;
^
db=> select site.name from site;
name
---------------
(1,mysitename)
(1 row)
第二个查询返回包含整个行的元组。使用Postgres 9.0.1。
编辑:通过请求定义站点的定义。我并不重要,这个怪癖适合任何表。
db=> \d site
Table "public.site"
Column | Type | Modifiers
--------+---------+---------------------------------------------------
id | integer | not null default nextval('site_id_seq'::regclass)
title | text | not null
其他提示
另请注意,在Postgresql 8.3中删除了隐式铸件,这意味着此行为不再起作用。几乎不可能在PostgreSQL 8.3及更高范围内意外获得此行为,因为元组不会自动转换为文本。
因此,在9.1中:
or_examples=# select c.name from comp_table_test c;
ERROR: column c.name does not exist
LINE 1: select c.name from comp_table_test c;
但是要获得这种行为,我们必须:
or_examples=# select name(c::text) from comp_table_test c;
或者,我们可以定义自己的名称函数,以comp_table_test类型中获取并返回我们想要的任何内容。
“名称”是 保留的关键词. 。因此,您应该“引用”使用它的关键字:
SELECT "name" FROM site;
过去,这已经解决了我过去的一些问题,您发布的代码也应该在不引用的情况下工作。另一方面
select site.name from site;
单词,因为您明确使用架构来解析列的名称
不隶属于 dba.stackexchange