私は現在SequelizeをORMとしてデータベースレイアウトを構築しています。私はUserとAppモデルからなるモデルレイアウトを持っています。 ユーザーはAppUsersテーブルを介して多くのAppsに属している(つまり、AppUsersにアクセスできる)。 Appはユーザーに属します。SequelizeのbelongsToMany関連の結果を大いに読み込んでください
私は私の実際の質問に今、このclassMethods: {
associate: function (models) {
User.belongsToMany(models.App, {
through: {
model: models.AppUser,
unique: true
},
foreignKey: 'user_id',
otherKey: 'app_id',
constraints: false,
as: 'apps'
});
User.hasMany(models.App, {
foreignKey: 'owner_id',
constraints: false,
scope: {
owner_type: 'user'
},
as: 'ownApps'
});
}
}
のようなこの
classMethods: {
associate: function (models) {
App.belongsTo(models.User, {
foreignKey: 'id',
constraints: false,
as: 'owner'
});
App.belongsToMany(models.User, {
through: {
model: models.AppUser,
unique: true
},
foreignKey: 'app_id',
otherKey: 'user_id',
constraints: false
});
}
}
とUserモデルのようにアプリケーションのモデルでこれを実装した
: 私はへの道を探していますユーザーがアクセスできるアプリケーション(つまりuser.getApps()
)を照会し、所有者情報を熱心に読み込んで、そのクエリへの応答に既に含まれているようにします。
User.belongsToMany(models.App, …
の関連ではinclude:
とscope:
で遊んでいましたが、どれも希望の結果を得ていませんでした。 これを行う方法はありますか、カスタムApp.findAll()
クエリを記述する必要はありますか?ありがとう!