我正在从事一些有关租用房屋和公寓的项目,当我需要根据房屋的功能(WiFi,安全性和其他人员)实施房屋过滤时,我已经达到了目的。一开始,我决定第一次尝试Sequelize ORM。诸如添加,创建,编辑之类的东西都可以正常工作,但是过滤部分是我遇到问题的地方。
我正在使用nodejs,express和postgresql。 我需要找到所有具有在功能ID数组中列出的功能的房屋。这是我尝试过的。在此示例中,我尝试获取具有ID 1、2和4的功能的房屋。
db.House.findAll({
include: [{
model: db.HouseFeature,
as: 'HouseFeatures',
where: {
featureId: {
[Op.contains]: [1, 2, 4] //<- array of featuresIds
}
}
}]
})
通过单个功能ID获取房屋效果很好,因为我在那里不使用Op.contains。 这是与此案例有关的一些关系:
House.hasMany(models.HouseFeature, { onDelete: 'CASCADE' });
HouseFeature.belongsTo(models.House);
HouseFeature包含featureId字段。 这是我得到的错误:
error: оператор не существует: integer @> unknown
at Connection.parseE (C:\***\server\node_modules\pg\lib\connection.js:601:11)
at Connection.parseMessage (C:\***\server\node_modules\pg\lib\connection.js:398:19)
at Socket.<anonymous> (C:\***\server\node_modules\pg\lib\connection.js:120:22)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'error',
length: 397,
severity: 'ОШИБКА',
code: '42883',
detail: undefined,
hint:
'Оператор с данными именем и типами аргументов не найден. Возможно, вам следует добавить явные приведения типов.',
position: '851',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file:
'd:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\parser\\parse_oper.c',
line: '731',
routine: 'op_error',
sql:
'SELECT "House"."id", "House"."title", "House"."description", "House"."price", "House"."address", "House"."lat", "House"."lon", "House"."kitchen", "House"."bathrooms", "House"."floor", "House"."totalFloors", "House"."people", "House"."area", "House"."bedrooms", "House"."trusted", "House"."createdAt", "House"."updatedAt", "House"."CityId", "House"."ComplexId", "House"."OwnerProfileId", "House"."HouseTypeId", "House"."RentTypeId", "HouseFeatures"."id" AS "HouseFeatures.id", "HouseFeatures"."featureId" AS "HouseFeatures.featureId", "HouseFeatures"."createdAt" AS "HouseFeatures.createdAt", "HouseFeatures"."updatedAt" AS "HouseFeatures.updatedAt", "HouseFeatures"."HouseId" AS "HouseFeatures.HouseId" FROM "Houses" AS "House" INNER JOIN "HouseFeatures" AS "HouseFeatures" ON "House"."id" = "HouseFeatures"."HouseId" AND "HouseFeatures"."featureId" @> \'1,2\';'
很抱歉在那里有一些俄罗斯人。
更新:
通过更改仅与一个HouseFeature相关的每个House,并更改该HouseFeature模型以存储FeatureId数组,我设法完成了我所需的工作。 Op.contains可以正常工作。
db.House.findAll({
include: [{
model: db.HouseFeature,
as: 'HouseFeature',
where: {
features: {
[Op.contains]: req.body.features
}
},
}]
})
// Associations
HouseFeature.belongsTo(models.House);
House.hasOne(models.HouseFeature, { onDelete: 'CASCADE' });
const HouseFeature = sequelize.define('HouseFeature', {
features: {
type: DataTypes.ARRAY(DataTypes.INTEGER)
}
}, {});
现在我有一个小问题。我可以通过某种方式将HouseFeature模型与Feature模型链接起来以获取特征图标图像并在以后命名吗?将功能ID存储在HouseFeature数组中。
答案 0 :(得分:0)
请检查Op.in
和Op.contains
之间的区别:
[Op.in]: [1, 2], // IN [1, 2]
[Op.contains]: [1, 2] // @> [1, 2] (PG array contains operator)
看起来HouseFeatures.featureId
是类型为integer
的PK,而不是postgres array
。
请尝试:
db.House.findAll({
include: [{
model: db.HouseFeature,
as: 'HouseFeatures',
where: {
featureId: {
[Op.in]: [1, 2, 3]
}
}
}]
})
甚至
db.House.findAll({
include: [{
model: db.HouseFeature,
as: 'HouseFeatures',
where: {
featureId: [1, 2, 3]
}
}]
})
代替