【发布时间】:2017-11-03 22:04:43
【问题描述】:
我定义了Track 和Artist 模型,关联如下:
db.Track.belongsToMany(db.Artist, {through: 'TracksArtists'});
db.Artist.belongsToMany(db.Track, {through: 'TracksArtists'});
我想搜索曲目并在结果中包含 Artist.name:
db.Track
findAll({
attributes: ['title','year'],
where: { title: { like: '%' + string + '%' } },
include: [{model: db.Artist, attributes: ['name']}]
})
.complete(function(err, tracks){ /*...*/});
不过,Sequelize 还在结果中包含 TracksArtists 参考表中的一行:
[{"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}}]}]
这是不必要的。我怎样才能让它不返回来自 TracksArtists 的信息,而不必自己删除它?
【问题讨论】:
-
试试
include: [{model: db.Artist, attributes: ['name'], through: {attributes: []}}] -
工作就像一个魅力,谢谢。
标签: sequelize.js