尝试使用sequelize内置的ORM方便,包括带有原始PostGIS查询的语法。
这个调用正确地加入了相关的promotionImage和类别表,就像我需要它一样,但是它并不像我需要那样对我的数据库进行地理查询:
promotion.findAll({
include: [{
model: promotionImage
}, {
model: category
}]
}).then(promoters => {
res.json(promoters)
}).catch(err => {
console.log('ERROR: ', err)
})
另一方面,此查询正确地在提供的lat / lng范围内找到“促销”,但它没有正确加入关联的promotionImage和类别表:
const query = '\
SELECT * \
FROM "promotions" \
WHERE "promotions"."location" && ST_MakeEnvelope(:southWestLng, :southWestLat, :northEastLng, :northEastLat)'
replacements = {
southWestLng,
southWestLat,
northEastLng,
northEastLat
}
db.sequelize.query(query, {
replacements,
type: sequelize.QueryTypes.SELECT,
model: promotion,
include: [{
model: promotionImage
}, {
model: category
}]
}).then(promoters => {
res.json(promoters)
}).catch(err => {
console.log('ERROR: ', err)
})
是否可以使用ST_MakeEnvelope
语法进行查询,并使用sequelize和postGIS包含其他模型的数据?
答案 0 :(得分:1)
Sequelize.query()
不支持include
,您可以Sequelize.fn()
使用findAll
promotion.findAll({
where: {
location: {
$overlap: db.sequelize.fn('ST_MakeEnvelope', southWestLng, southWestLat, northEastLng, northEastLat)
}
},
include: [{
model: promotionImage
}, {
model: category
}]
}).then(promoters => {
res.json(promoters)
}).catch(err => {
console.log('ERROR: ', err)
})