Knex: 选择计算列?

创建于 2014-12-18  ·  3评论  ·  资料来源: knex/knex

我正在尝试使用 postgresql earthdistance 扩展编写一个查询,我想返回一个计算列,但我一直收到一个错误:缺少表“670906,-79”的 FROM 子句条目。 这大概是因为解析器认为一个点将表名与列名分开,但在我的情况下,我正在执行计算(请参阅下面的示例查询)。 有没有办法解决这个问题?

Possibly unhandled error: missing FROM-clause entry for table "670906,-79"
    at Connection.parseE (/usr/local/lib/node_modules/pg/lib/connection.js:534:11)
    at Connection.parseMessage (/usr/local/lib/node_modules/pg/lib/connection.js:361:17)
    at Socket.<anonymous> (/usr/local/lib/node_modules/pg/lib/connection.js:105:22)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:764:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:426:10)
    at emitReadable (_stream_readable.js:422:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
  var columns = [
    'id',
    '(earth_distance(ll_to_earth(lat, lng), ll_to_earth(43.670906, -79.393331)) as distance)'
  ];
  var result = knex
    .select(columns)
    .from('stores')
    .whereRaw('earth_box(ll_to_earth(43.670906, -79.393331), ?) @> ll_to_earth(lat, lng)', [radiusInStatuteMiles])
question

最有用的评论

您应该为计算列使用knex.raw ,这样它就不会被引用。 以下应该工作:

var columns = [
  'id',
  knex.raw('earth_distance(ll_to_earth(lat, lng), ll_to_earth(43.670906, -79.393331)) as distance')
];
var result = knex
.select(columns)
.from('stores')
.whereRaw('earth_box(ll_to_earth(43.670906, -79.393331), ?) @> ll_to_earth(lat, lng)', [radiusInStatuteMiles])

所有3条评论

您应该为计算列使用knex.raw ,这样它就不会被引用。 以下应该工作:

var columns = [
  'id',
  knex.raw('earth_distance(ll_to_earth(lat, lng), ll_to_earth(43.670906, -79.393331)) as distance')
];
var result = knex
.select(columns)
.from('stores')
.whereRaw('earth_box(ll_to_earth(43.670906, -79.393331), ?) @> ll_to_earth(lat, lng)', [radiusInStatuteMiles])

谢谢! 正是我需要的。 不知何故没有想到使用knex.raw

别客气!

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

hyperh picture hyperh  ·  3评论

koskimas picture koskimas  ·  3评论

fsebbah picture fsebbah  ·  3评论

mishitpatel picture mishitpatel  ·  3评论

saurabhghewari picture saurabhghewari  ·  3评论