Sequelize includes a lookup table row as a result when using include

I have models defined Track

and Artist

with an association like this:

db.Track.belongsToMany(db.Artist, {through: 'TracksArtists'});
db.Artist.belongsToMany(db.Track, {through: 'TracksArtists'});

      

I want to search for tracks and include Artist.name in it:

db.Track
    findAll({ 
        attributes: ['title','year'], 
        where: { title: { like: '%' + string + '%' } },
        include: [{model: db.Artist, attributes: ['name']}]
    })
    .complete(function(err, tracks){ /*...*/});

      

However, Sequelize also includes a row from the TracksArtists link table in the results:

[{"title":"Nightcall","year":2010,"Artists":[{"name":"Kavinsky","TracksArtists":{"createdAt":"2015-01-13T18:41:31.850Z","updatedAt":"2015-01-13T18:41:31.850Z","ArtistId":1,"TrackId":1}}]}]

      

what is not needed. How can I get it to not return information from TracksArtists, rather than delete it myself?

+3


source to share


3 answers


You can disable the connection table attributes by passing an empty array of attributes, for example:



include: [{model: db.Artist, attributes: ['name'], through: {attributes: []}}]

+9


source


I have models defined Faq

and Artist

with an association like this:

Faq.belongsToMany(Version, { through: FaqVersion, foreignKey: 'faqId' });
Version.belongsToMany(Faq, { through: FaqVersion, foreignKey: 'verId' });

      

I faced the same problem with the author, but I add:

models.Version.findAll({
  raw: true,
  attributes: ['id', 'version'],
  include: [{
    model: models.Faq,
    attributes: [],
    through: { attributes: [] },
    where: {
    id: faq.id
    }
 }]
})

      



However, Sequelize also includes a row from the FaqVersion lookup table in the results:

Faqs.FaqVersion.createdAt:"2017-01-10T05:22:06.000Z",
Faqs.FaqVersion.faqId:2,
Faqs.FaqVersion.id:3,
Faqs.FaqVersion.updatedAt:"2017-01-10T05:22:06.000Z",
Faqs.FaqVersion.verId:2,
id:2,
version:"5.2.6"

      

I think it through

doesn't work

+1


source


just use the syntax like this:

include: [
            {
                model: ModelName,
                through: {attributes: []}
            }
    ]

      

0


source







All Articles