簡體   English   中英

在sequelize.js中使用include時,如何將所有列放在一行中?

[英]How to bring all columns in a single row when using include in sequelize.js?

我想在單行中構造查詢列。 我在續集中使用了以下代碼。 請幫助我解決這個問題。

return Entity.userAddress.findAll({            
             where: {
                'user_id': userAddress.user_id
            }, 

            include:[{
                model: Entity.masterCountry,
                required: true,
                attributes: ['country_id', 'country_name'],
                nested: true
            },
            {
                model: Entity.masterState,
                required: true,
                attributes: ['state_id', 'state_name']
            }]

        })
        .then((roleList) => {
            return roleList;
        })


This is my result when I have used above code.   
<pre>
{
    "code": 0,
    "status": "success",
    "data": [
        {
            "user_address_id": "04cec50e-4a2f-445f-b728-5ec8e914e322",
            "user_id": "657d1823-1601-4072-863e-fd878f517d66",
            "address1": "test1",
            "address2": "test2",
            "city_name": "chennai",
"masterState": {
                "state_id": "b20dd34e-999e-445a-9b90-60246eefbd7e",
                "state_name": "Alabama",
                "short_name": "AL",
            },
            "masterCountry": {
                "country_id": "aa656249-e0cf-4b40-ac35-8a5672475670",
                "country_name": "United States"
            }
        }
    ]
}
</pre>

預期結果應如下所示。 請幫助我解決這個問題。

{
        "code": 0,
        "status": "success",
        "data": [
            {
                "user_address_id": "04cec50e-4a2f-445f-b728-5ec8e914e322",
                "user_id": "657d1823-1601-4072-863e-fd878f517d66",
                "address1": "test1",
                "address2": "test2",
                "city_name": "chennai",
                 "short_name": "AL"
                "country_id": "aa656249-e0cf-4b40-ac35-8a5672475670",
                "country_name": "United States"
            }
        ]
    }

為此,需要使用sequelize.literal ,以便根據需要指定查詢的“選擇”部分。

return Entity.userAddress.findAll({            
         where: {
            'user_id': userAddress.user_id
        }, 
        attributes: [
          // your order attributes from userAddress here
          [db.sequelize.literal('"masterCountry"."country_id"'), 'country_id'],
          [db.sequelize.literal('"masterCountry"."country_name"'), 'country_name'],
          [db.sequelize.literal('"masterState"."state_id"'), 'state_id'],
          [db.sequelize.literal('"masterState"."state_name"'), 'state_name'],
        ],
        include:[{
            model: Entity.masterCountry,
            required: true,
            attributes: [],
            nested: true
        },
        {
            model: Entity.masterState,
            required: true,
            attributes: []
        }]

    })
    .then((roleList) => {
        return roleList;
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM