简体   繁体   English

Sequelize M:N 关联操作

[英]Sequelize M:N association manipulation

I have a sequelize schema using postgre DB我有一个使用 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});

And I have successfully created this :我已经成功创建了这个:

await CodePostal.create({
       codePostal: "37340",
       communes: [
           {
               codeCommune: "37002",
               libelleCommune:"Ambillou",
               actif: true
           },
           {
               codeCommune:"37013",
               libelleCommune: "Avrillé-les-Ponceaux",
               actif: true
           }
       ]
   }, {include: Commune}).then ...

Now I want to list all the data like this :现在我想列出所有这样的数据:

CodePostal.findAll({raw:true,include:[{model: Commune},nest:true}).then ...

Expected output :预期输出:

{codePostal: "37340",
communes: [
   {codeCommune: "37002",libelleCommune: "Ambillou",actif: true},
   {codeCommune: "37013",libelleCommune: "Avrillé-les-Ponceaux",actif: true}
]}

But I have this :但我有这个:

[ { 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] }
     }
]

For each commune, sequelize joins the code postal, insted of listing the communes in a array for each codePostal.对于每个公社,sequelize 连接代码邮政,从而为每个代码邮政在数组中列出公社。

Can someone help me to achieve this result ?有人能帮我实现这个结果吗?

raw: true will return the data as DB returns in a plain response. raw: true将返回数据,因为 DB 在一个简单的响应中返回。 That means it will return each association as 1 row.这意味着它将每个关联返回为 1 行。 However, your expected output is compounded into 1 array for the same codepostal .但是,对于相同的codepostal ,您的预期输出复合到 1 个数组中。

By default (without raw: true ), findAll should return as your expected output and nest: true is not necessary either.默认情况下(没有raw: true ), findAll应该作为您预期的输出返回,并且nest: true也不是必需的。

CodePostal.findAll({
    include:[{model: Commune}]
})

Also, if you don't need attributes from the through table, you can add these option to get cleaner response.此外,如果您不需要through表中的属性,您可以添加这些选项以获得更清晰的响应。

CodePostal.findAll({
    include:[{
        model: Commune,
        through: {
            attributes: []
        }
    }]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM