matteo.rebeschi:
多対多の関係で接続されたテーブルから値を持つ行のリストをフィルタリング:私は、次のような結果を得るためのより良い方法があるかどうかを知りたいです。私の例の場合では、特定の機器を備えていた曲です。
ここに私の歌のモデルは次のとおりです。
module.exports = function(sequelize, DataTypes) {
var Song = sequelize.define('songs', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING(50),
allowNull: false
},
}, {
tableName: 'songs',
timestamps: false
});
Song.associate = function (models) {
Song.belongsToMany(models.instruments, {through: 'songs_instruments', foreignKey: 'song_id'})
};
return Song
};
ここに私の楽器のモデルは次のとおりです。
module.exports = function(sequelize, DataTypes) {
var Instrument = sequelize.define('instruments', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(50),
allowNull: false
}
}, {
tableName: 'instruments',
timestamps: false,
});
Instrument.associate = function (models) {
Instrument.belongsToMany(models.songs, {through: 'songs_instruments', foreignKey: 'instrument_id'})
};
return Instrument
};
ここで結合テーブルのモデルは以下のとおりです。
module.exports = function(sequelize, DataTypes) {
return sequelize.define('songs_instruments', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
song_id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
references: {
model: 'songs',
key: 'id'
}
},
instrument_id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
references: {
model: 'instruments',
key: 'id'
}
}
}, {
tableName: 'songs_instruments',
timestamps: false
});
};
そして、ここで私は現在、(私のフェッチをやっている方法ですargs['instruments']
私は歌をフィルタリングする機器のリストが含まれています):
const instruments = await db.instruments.findAll({
where: {name: args['instruments']}
})
const songs_instruments = await db.songs_instruments.findAll({
where: {instrument_id: instruments.map(instrument => instrument.id)}
})
const songs = await db.songs.findAll({
where: {id: songs_instruments.map(song => song.song_id)}
})
return songs
これが正常に動作し、最終的に私は私が期待する結果を得る一方で、私はありません助けが、このようにそれをやって、データベースを照会するより効率的な方法があるかどうか不思議は3つのクエリの合計を実行させることができます:
Executing (default): SELECT `id`, `name` FROM `instruments` AS `instruments` WHERE `instruments`.`name` IN ('Vocals', 'Trumpet', 'Guitar');
Executing (default): SELECT `id`, `song_id`, `instrument_id` FROM `songs_instruments` AS `songs_instruments` WHERE `songs_instruments`.`instrument_id` IN (1, 3, 7);
Executing (default): SELECT `id`, `title` FROM `songs` AS `songs` WHERE `songs`.`id` IN (1, 1, 2, 2, 3, 3);
私はSequelize、私の目標を達成するので、私はおそらく間違って/次善の方法に非常に新しいです。
グレッグBelyea:
「楽器」...あなたがする必要はありません:ああ、私は完全に...それはそうあなたがそれを行うことができます..この接続エイリアスに以下のようなとして追加して、などと歌で同じことを行う。..遅かったことを逃しましたレコードを参加フェッチ..
Instrument.associate = function (models) {
Instrument.belongsToMany(models.songs, {through: 'songs_instruments', as: 'songs', foreignKey: 'instrument_id', otherKey: 'song_id'})
};
const instruments = await db.instruments.findAll({
where: {name: args['instruments']},
{
include: [{
model: db.songs,
as: 'songs',
through: db.songs_instruments,
}]
}
});
以下が含まの線に沿って何か...
const songs = await db.songs.findAll({
include: [{
model: db.instruments,
as: 'instruments',
through: db.songs_instruments,
where: {name: args['instruments']}
}],
});