{pg-promise} postgres - 如何将类型 int[] 转换为与 POINT() 兼容
{pg-promise} postgres - how to convert type int[] to be compatible with POINT()
所以我使用 pg-promise
插入类型 POINT
的列。但它给了我以下错误:
function point(integer[]) does not exist
我将值作为数组传递。我应该更改什么才能使其正常工作?
一些代码(不确定是否有用):
simplified_query = `$${counter++}:name = POINT($${counter++})`
fields =
[
"geolocation",
[10, 10]
]
根据Custom Type Formatting,如果你的field
是['geolocation', [10, 10]]
,第一个值是列名,你可以使用下面的函数:
function asPoint(field) {
return {
rawType: true,
toPostgres: () => pgp.as.format(':name = POINT(:csv)', field)
};
}
然后您可以使用 asPoint(field)
作为查询格式参数:
const field = ['geolocation', [10, 10]];
db.any('SELECT * FROM table WHERE ', [asPoint(field)])
//=> SELECT * FROM table WHERE "geolocation" = POINT(10, 10)
或者,您的 field
可以是自定义类型 class,它可以显式或通过原型实现 Custom Type Formatting,在这种情况下,它可以直接用作格式化参数.
所以我使用 pg-promise
插入类型 POINT
的列。但它给了我以下错误:
function point(integer[]) does not exist
我将值作为数组传递。我应该更改什么才能使其正常工作?
一些代码(不确定是否有用):
simplified_query = `$${counter++}:name = POINT($${counter++})`
fields =
[
"geolocation",
[10, 10]
]
根据Custom Type Formatting,如果你的field
是['geolocation', [10, 10]]
,第一个值是列名,你可以使用下面的函数:
function asPoint(field) {
return {
rawType: true,
toPostgres: () => pgp.as.format(':name = POINT(:csv)', field)
};
}
然后您可以使用 asPoint(field)
作为查询格式参数:
const field = ['geolocation', [10, 10]];
db.any('SELECT * FROM table WHERE ', [asPoint(field)])
//=> SELECT * FROM table WHERE "geolocation" = POINT(10, 10)
或者,您的 field
可以是自定义类型 class,它可以显式或通过原型实现 Custom Type Formatting,在这种情况下,它可以直接用作格式化参数.