如果我的联接表的额外列是第三张表的外键(即options),则可以在查询中包括该表的其他列(即options.name,options.description ..)。 )?
协会:
let User = sequelize.define('user', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4
}
});
User.associate = models => {
User.belongsToMany(
models.mail_settings,
{
through: 'user_mail_settings',
foreignKey: 'user_id'
}
);
}
let MailSettings = sequelize.define('mail_settings', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4
},
name: {
type: DataTypes.STRING(100)
}
});
MailSettings.associate = (models) => {
MailSettings.belongsToMany(
models.user,
{
through: 'user_mail_settings',
foreignKey: 'mail_settings_id'
}
)
};
var PrivacyOptions = sequelize.define('mail_options', {
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4
}
});
var UserMailSettings = sequelize.define('user_mail_settings', {
});
UserMailSettings.associate = (models) => {
UserMailSettings.belongsTo(
models.mail_options,
{
foreignKey: 'fk_selection_user_mail_settings',
targetKey: 'id'
}
)
};
我知道可以通过将其添加到我的用户查询中来获取该外键:
include: [{
model: models.mail_settings,
attributes: ['id', 'name'],
through: {
attributes: ['fk_selection_user_mail_settings']
},
where: {
name: 'some_query_here'
}
}]
但是我该如何更进一步,并从mail_options模型中获取更多列?