How to remove the table name at the child?

Viewed 38
const result = await Article.findAll({
        include:{
            model:User,
            attributes:[['username','author']]
        },
        where:{deleted:0},
        attributes: ['id','title','description','keywords','content','tabId','createdAt','updatedAt'],
        order:[
            ['id','desc']
        ],
        raw:true
    })
    console.log(result)

Search results here

I added a raw:true make the child param on the same layer.

but it got a prefix User..

how to remove it.

1 Answers

I don't think it is possible to remove the User prefix since you have included a join inside your include option.

Suggestions :

  1. Add nest: true instead of the option raw:true for your query, which will convert it from an alias to an object. This will help you achieve a cleaner result.
const result = await Article.findAll({
  include: {
    model: User,
    attributes: [['username', 'author']],
  },
  where: { deleted: 0 },
  attributes: [
    'id',
    'title',
    'description',
    'keywords',
    'content',
    'tabId',
    'createdAt',
    'updatedAt',
  ],
  order: [['id', 'desc']],
  nest: true, <=======
});

  1. You can add an alias for your join, this way you have control over your prefix by providing the as operator inside your associations please visit the documentation for more info
Related