简体   繁体   中英

Sequelize: How can I prevent virtual columns from included relation from appearing in query result?

I have a query that looks like this:

  return GroupMember.findOne({ where: { gid: gid, member_id: uid }, include: [ { model: User, as: 'member', attributes: ['display_name'], }, { model: Group, as: 'group', attributes: ['name'], } ] }); 

So, for the "member" relation, I am requesting only the 'display_name" column. However, the User model has 3 virtual columns declared in it, and they are always present in the query result even though I asked for only 'display_name'. How do I prevent the virtual columns from being present in the result?

So for excluding virtual columns, you have to use the exclude property the attributes field, so the new query should be like this

    return GroupMember.findOne({
    where: { gid: gid, member_id: uid },
    include: [
        {
            model: User,
            as: 'member',
            attributes: { include: ['display_name'], exclude: ['virtual_columne_name1', 'virtual_columne_name2', 'virtual_columne_name3']},
        },
        {
            model: Group,
            as: 'group',
            attributes: ['name'],
        }
    ]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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