【发布时间】:2021-11-16 06:29:32
【问题描述】:
我有一个使用 postgre DB 的 sequelize 架构
export const Commune = sq.define("commune",{
codeCommune: {
type: DataTypes.STRING(5),
allowNull: false,
primaryKey: true
},
libelleCommune: {
type: DataTypes.STRING,
allowNull:false
},
actif: {
type: DataTypes.BOOLEAN,
allowNull:true
}
},{timestamps: false,modelName:"Commune"});
export const CodePostal = sq.define("codePostal",{
codePostal: {
type: DataTypes.STRING(5),
allowNull: false,
primaryKey: true
}
},{timestamps: false,modelName:"CodePostal"});
export const R_Commune_CodePostal = sq.define("R_Commune_CodePostal",{},{timestamps:false});
CodePostal.belongsToMany(Commune,{through: R_Commune_CodePostal});
Commune.belongsToMany(CodePostal,{through: R_Commune_CodePostal});
我已经成功创建了这个:
await CodePostal.create({
codePostal: "37340",
communes: [
{
codeCommune: "37002",
libelleCommune:"Ambillou",
actif: true
},
{
codeCommune:"37013",
libelleCommune: "Avrillé-les-Ponceaux",
actif: true
}
]
}, {include: Commune}).then ...
现在我想像这样列出所有数据:
CodePostal.findAll({raw:true,include:[{model: Commune},nest:true}).then ...
预期输出:
{codePostal: "37340",
communes: [
{codeCommune: "37002",libelleCommune: "Ambillou",actif: true},
{codeCommune: "37013",libelleCommune: "Avrillé-les-Ponceaux",actif: true}
]}
但我有这个:
[ { codepostal: '37340',
communes: {
codeCommune: '37002',
libelleCommune: 'Ambillou',
actif: true,
R_Commune_CodePostal: [Object] } },
{ codepostal: '37340',
communes: {
codeCommune: '37013',
libelleCommune: 'Avrillé-les-Ponceaux',
actif: true,
R_Commune_CodePostal: [Object] }
}
]
对于每个公社,sequelize 加入邮政编码,用于在每个邮政编码的数组中列出公社。
有人可以帮我实现这个结果吗?
【问题讨论】:
标签: javascript node.js typescript sequelize.js sequelize-typescript