Fuj*_*uji 5 postgresql node.js express sequelize.js
我正在做一些关于出租房屋和公寓的项目,我已经达到了需要根据房屋拥有的功能(wifi、安全和其他工作人员)实施过滤房屋的程度。一开始我决定第一次尝试 Sequelize ORM。像添加、创建、编辑这样的东西工作正常,但过滤部分是我遇到的一些问题。
\n\n我正在使用 nodejs、express 和 postgresql。\n我需要找到所有具有功能 ID 数组中列出的功能的房屋。这是我尝试过的。在这个例子中,我试图获取具有 id 1、2 和 4 的特征的房屋。
\n\ndb.House.findAll({\n include: [{\n model: db.HouseFeature, \n as: \'HouseFeatures\',\n where: {\n featureId: {\n [Op.contains]: [1, 2, 4] //<- array of featuresIds\n } \n }\n }]\n})\n
Run Code Online (Sandbox Code Playgroud)\n\n通过单个功能 ID 获取房屋效果很好,因为我在那里不使用 Op.contains。\n以下是与此案例相关的一些关系:
\n\nHouse.hasMany(models.HouseFeature, { onDelete: \'CASCADE\' });\nHouseFeature.belongsTo(models.House);\n
Run Code Online (Sandbox Code Playgroud)\n\nHouseFeature 包含 featureId 字段。\n这是我收到的错误:
\n\nerror: \xd0\xbe\xd0\xbf\xd0\xb5\xd1\x80\xd0\xb0\xd1\x82\xd0\xbe\xd1\x80 \xd0\xbd\xd0\xb5 \xd1\x81\xd1\x83\xd1\x89\xd0\xb5\xd1\x81\xd1\x82\xd0\xb2\xd1\x83\xd0\xb5\xd1\x82: integer @> unknown\n at Connection.parseE (C:\\***\\server\\node_modules\\pg\\lib\\connection.js:601:11)\n at Connection.parseMessage (C:\\***\\server\\node_modules\\pg\\lib\\connection.js:398:19)\n at Socket.<anonymous> (C:\\***\\server\\node_modules\\pg\\lib\\connection.js:120:22)\n at Socket.emit (events.js:182:13)\n at addChunk (_stream_readable.js:283:12)\n at readableAddChunk (_stream_readable.js:264:11)\n at Socket.Readable.push (_stream_readable.js:219:10)\n at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)\n name: \'error\',\n length: 397,\n severity: \'\xd0\x9e\xd0\xa8\xd0\x98\xd0\x91\xd0\x9a\xd0\x90\',\n code: \'42883\',\n detail: undefined,\n hint:\n \'\xd0\x9e\xd0\xbf\xd0\xb5\xd1\x80\xd0\xb0\xd1\x82\xd0\xbe\xd1\x80 \xd1\x81 \xd0\xb4\xd0\xb0\xd0\xbd\xd0\xbd\xd1\x8b\xd0\xbc\xd0\xb8 \xd0\xb8\xd0\xbc\xd0\xb5\xd0\xbd\xd0\xb5\xd0\xbc \xd0\xb8 \xd1\x82\xd0\xb8\xd0\xbf\xd0\xb0\xd0\xbc\xd0\xb8 \xd0\xb0\xd1\x80\xd0\xb3\xd1\x83\xd0\xbc\xd0\xb5\xd0\xbd\xd1\x82\xd0\xbe\xd0\xb2 \xd0\xbd\xd0\xb5 \xd0\xbd\xd0\xb0\xd0\xb9\xd0\xb4\xd0\xb5\xd0\xbd. \xd0\x92\xd0\xbe\xd0\xb7\xd0\xbc\xd0\xbe\xd0\xb6\xd0\xbd\xd0\xbe, \xd0\xb2\xd0\xb0\xd0\xbc \xd1\x81\xd0\xbb\xd0\xb5\xd0\xb4\xd1\x83\xd0\xb5\xd1\x82 \xd0\xb4\xd0\xbe\xd0\xb1\xd0\xb0\xd0\xb2\xd0\xb8\xd1\x82\xd1\x8c \xd1\x8f\xd0\xb2\xd0\xbd\xd1\x8b\xd0\xb5 \xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd0\xb4\xd0\xb5\xd0\xbd\xd0\xb8\xd1\x8f \xd1\x82\xd0\xb8\xd0\xbf\xd0\xbe\xd0\xb2.\',\n position: \'851\',\n internalPosition: undefined,\n internalQuery: undefined,\n where: undefined,\n schema: undefined,\n table: undefined,\n column: undefined,\n dataType: undefined,\n constraint: undefined,\n file:\n \'d:\\\\pginstaller.auto\\\\postgres.windows-x64\\\\src\\\\backend\\\\parser\\\\parse_oper.c\',\n line: \'731\',\n routine: \'op_error\',\n sql:\n \'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\\\';\'\n
Run Code Online (Sandbox Code Playgroud)\n\n抱歉那里有一些俄语。
\n\n更新:
\n\n我已经成功地通过更改仅与一个 HouseFeature 相关的每个 House,并更改该 HouseFeature 模型来存储 featureId 数组来完成我需要的操作。Op.contains 工作正常。
\n\n db.House.findAll({\n include: [{\n model: db.HouseFeature, \n as: \'HouseFeature\',\n where: {\n features: {\n [Op.contains]: req.body.features\n } \n },\n }] \n })\n // Associations\n HouseFeature.belongsTo(models.House);\n House.hasOne(models.HouseFeature, { onDelete: \'CASCADE\' });\n\n const HouseFeature = sequelize.define(\'HouseFeature\', {\n features: {\n type: DataTypes.ARRAY(DataTypes.INTEGER)\n }\n }, {});\n\n
Run Code Online (Sandbox Code Playgroud)\n\n现在我有一个小问题。我可以以某种方式将 HouseFeature 模型与功能模型链接起来以稍后获取功能图标图像和名称吗?功能 id 存储在 HouseFeature 数组中。
\nOp.in
请检查和之间的区别Op.contains
:
[Op.in]: [1, 2], // IN [1, 2]
[Op.contains]: [1, 2] // @> [1, 2] (PG array contains operator)
看上去HouseFeatures.featureId
是跟类型的PK integer
,而不是postgres array
。
请尝试:
db.House.findAll({
include: [{
model: db.HouseFeature,
as: 'HouseFeatures',
where: {
featureId: {
[Op.in]: [1, 2, 3]
}
}
}]
})
Run Code Online (Sandbox Code Playgroud)
甚至
db.House.findAll({
include: [{
model: db.HouseFeature,
as: 'HouseFeatures',
where: {
featureId: [1, 2, 3]
}
}]
})
Run Code Online (Sandbox Code Playgroud)
反而
归档时间: |
|
查看次数: |
9309 次 |
最近记录: |