代码之家  ›  专栏  ›  技术社区  ›  A. L

pg promise postgres-如何将类型int[]转换为与point()兼容

  •  1
  • A. L  · 技术社区  · 7 年前

    所以我在用 pg-promise 插入到类型中 POINT 列。但它给了我以下错误:

    function point(integer[]) does not exist
    

    我将值作为数组传递。我应该做些什么来让它工作?

    一些代码(不确定是否有用):

    simplified_query = `$${counter++}:name = POINT($${counter++})`
    
    fields =
    [
        "geolocation",
        [10, 10]
    ]
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   vitaly-t    7 年前

    按照 Custom Type Formatting 如果你 field ['geolocation', [10, 10]] ,第一个值是列名,可以使用以下函数:

    function asPoint(field) {
        return {
            rawType: true,
            toPostgres: () => pgp.as.format('$1:name = POINT($2:csv)', field)
        };
    }
    

    然后你可以用 asPoint(field) 作为查询格式参数:

    const field = ['geolocation', [10, 10]];
    db.any('SELECT * FROM table WHERE $1', [asPoint(field)])
    //=> SELECT * FROM table WHERE "geolocation" = POINT(10, 10)
    

    或者,您的 领域 可以是实现的自定义类型类 自定义类型格式 无论是显式的还是通过原型,在这种情况下,它可以直接用作格式化参数。