在深层链接中将 jsonb 转换为 bigint 时出现问题

Issue while casting jsonb to bigint in deeplink

虽然在同一个数据库中投射工作正常,但在使用深度链接时失败

在同一个数据库中工作正常(通过此查询获取结果):

SELECT id, 
       total_inventory, 
       hotel_id, 
       ( room_type -> 'id' ) :: bigint AS room_type_id, 
       created_by, 
       created_date, 
       modified_by, 
       modified_date 
FROM   hotel_inventory; 

与其他数据库连接时不工作(使用深层链接)(此查询出错):

INSERT INTO hotel_inventory 
            ( 
                        id, 
                        total_inventory, 
                        hotel_id, 
                        room_type_id, 
                        created_by, 
                        created_date, 
                        modified_by, 
                        modified_date 
            ) 
SELECT * 
FROM   dblink('demopostgres', 'SELECT id, total_inventory, hotel_id, (room_type -> 'id')::bigint as room_type_id,  created_by, created_date, modified_by, modified_date FROM hotel_inventory') 
AS data(id bigint, total_inventory integer, hotel_id bigint, room_type_id bigint, created_by jsonb, created_date timestamp without time zone, modified_by jsonb, modified_date timestamp without time zone);

错误:

ERROR:  syntax error at or near "id"
LINE 3: ...ECT id, total_inventory, hotel_id, (room_type -> 'id')::bigi...
                                                             ^
SQL state: 42601
Character: 221

您需要将单引号加倍以避免此类错误:

dblink('demopostgres',
       'SELECT . . . (room_type -> ''id'')::bigint as room_type_id . . . '
      )

问题是一个简单的解析错误。单引号结束字符串 - 因此错误。双单引号是在字符串中放置单引号的标准方法,尽管不同的数据库通常支持其他方法(例如反斜杠)。