Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

sequelize mysql 多表联查属性名变成了‘db_name.attribute’ 如何改成 'attribute'?

数据是源于第三方api同步过来的数据,所以我不想给他们在mysql上设置关联关系
所以我直接在 service 里设置关联关系

XbMoneyAllocation.belongsTo(XbHasClass, {?foreignKey: 'contractProductId', targetKey: 'id' })
XbHasClass.belongsTo(XbProduct, {?foreignKey: 'productId', targetKey: 'id' })

下面是执行查询

let res = await XbMoneyAllocation.findAll({
    raw: true,
    where: {
        fundsChangeHistoryId: { [Op.in]: ids }
    },
    attributes: ['changeAmount'],
    include: [{
        model: XbHasClass,
        attributes: ['productId'],
        include: [{
            model: XbProduct,
            attributes: ['belongFeeTypeId']
        }]
    }]
 })

这里得到的是结果
这里有两个问题需要解决
1、属性带上了表名
2、xb_has_class.productIdxb_has_class.xb_product.id 重复出现了

{
    "changeAmount": 1734,
    "xb_has_class.productId": 454905,
    "xb_has_class.xb_product.id": 454905,
    "xb_has_class.xb_product.belongFeeTypeId": 2019438
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...