它的PostgreSQL列不存在

时间:2014-12-04 21:49:34

标签: postgresql

我有以下PostgreSQL查询...

WITH update_parts (id,id_finish) 
AS (VALUES (1,42), (2,42), (3,30), (4,30)) 
UPDATE parts SET id = up.id, id_metal = up.id_metal 
FROM update_parts up WHERE up.id = parts.id;

......我收到了错误......

  

列up.id_metal不存在LINE 1

我已经验证我已连接到正确的数据库,正确的表并且该列存在。我只是部分肯定我通过以下方式设置的权限...

GRANT ALL(id_metal) ON parts TO public;

...和...

GRANT ALL(id_metal) ON parts TO postgres;

我计划在找出问题后将权限限制为SELECTUPDATEDELETEINSERT。为了澄清,它是“parts”表和“id_metal”列。

如何解决此错误并UPDATE行?

更新权限后,我不确定还需要考虑什么?

我使用以下查询来获取有关列的信息...

SELECT * FROM information_schema.columns 
WHERE table_schema NOT IN ('pg_catalog', 'information_schema') 
AND column_name='id_finish';

...返回......

  

数组(

[table_catalog] => [database name]

[table_schema] => public

[table_name] => parts

[column_name] => id_metal

[ordinal_position] => 7

[column_default] => 

[is_nullable] => YES

[data_type] => bigint

[character_maximum_length] => 

[character_octet_length] => 

[numeric_precision] => 64

[numeric_precision_radix] => 2

[numeric_scale] => 0

[datetime_precision] => 

[interval_type] => 

[interval_precision] => 

[character_set_catalog] => 

[character_set_schema] => 

[character_set_name] => 

[collation_catalog] => 

[collation_schema] => 

[collation_name] => 

[domain_catalog] => 

[domain_schema] => 

[domain_name] => 

[udt_catalog] => [database name]

[udt_schema] => pg_catalog

[udt_name] => int8

[scope_catalog] => 

[scope_schema] => 

[scope_name] => 

[maximum_cardinality] => 

[dtd_identifier] => 7

[is_self_referencing] => NO

[is_identity] => NO

[identity_generation] => 

[identity_start] => 

[identity_increment] => 

[identity_maximum] => 

[identity_minimum] => 

[identity_cycle] => 

[is_generated] => NEVER

[generation_expression] => 

[is_updatable] => YES
     

1 个答案:

答案 0 :(得分:0)

你正在经历许多具有权限的箍,但问题是简单的语法:

WITH update_parts (id, id_finish) AS (
    VALUES (1,42), (2,42), (3,30), (4,30)
) 
UPDATE parts SET id_metal = up.id_finish
FROM update_parts up WHERE up.id = parts.id;

在您的CTE中,您可以定义列ID和 id_finish ,因此您的主查询应引用相同的 up.id_finish ,而不是零件表中的id_metal。

此外,使用parts.id的值更新update_parts.id是没有用的,因为根据定义它们是相同的:WHERE up.id = parts.id