Limiting eager loading in sequilize
Есть две модели AdminModel и AdminLastEntrancesModel. В первой просто список администраторов, во второй список их последних входов. Мне надо получать с пагинацией админов и дату их последнего входа. Модель админ
class AdminModel extends Model {
static superAdmin() {
return 'super_admin'
}
static admin() {
return 'admin'
}
};
AdminModel.init(
{
email: DataTypes.STRING(150),
password: DataTypes.STRING(100),
nickname: DataTypes.STRING(100),
first_name: DataTypes.STRING(100),
last_name: DataTypes.STRING(100),
phone: {
type: DataTypes.STRING(20),
allowNull: true,
},
role: DataTypes.STRING(20),
ban: DataTypes.BOOLEAN,
createdAt: {type: DataTypes.DATE, field: 'created_at'},
updatedAt: {type: DataTypes.DATE, field: 'updated_at'},
}, {
defaultScope: {
attributes: { exclude: ['password'] },
},
scopes: {
withPassword: {
attributes: { },
}
},
tableName: 'admins',
timestamps: true,
underscored: true,
sequelize,
modelName: 'Admin',
});
AdminModel.hasMany(AdminLastEntrancesModel, {
foreignKey: 'admin_id'
});
AdminLastEntrancesModel.belongsTo(AdminModel, {
foreignKey: 'admin_id'
})
module.exports = {
AdminModel: AdminModel,
}
Модель AdminLastEntrancesModel\
class AdminLastEntrancesModel extends Model {
static associate(models) {
}
createEntrances = async (ip, device, userId) => {
await AdminLastEntrancesModel.create({
ip: ip,
device: device,
admin_id: userId
})
}
};
AdminLastEntrancesModel.init({
admin_id: {
type: DataTypes.INTEGER,
references: {
model: {
tableName: 'admins',
},
key: 'id'
},
},
device: DataTypes.STRING,
ip: DataTypes.STRING,
createdAt: {type: DataTypes.DATE, field: 'created_at'},
updatedAt: {type: DataTypes.DATE, field: 'updated_at'},
}, {
tableName: 'admin_last_entrances',
timestamps: true,
underscored: true,
sequelize,
modelName: 'AdminLastEntrancesModel'
});
module.exports = {
AdminLastEntrancesModel: AdminLastEntrancesModel
}
Сам запрос
await AdminModel.findAndCountAll({
where: whereClause,
include: [{
model: AdminLastEntrancesModel,
where: whereForChildModel,
required: true,
duplicating: false,
order: [['created_at', 'DESC']],
}],
limit: limit,
offset: offset,
order: [[{model: AdminLastEntrancesModel}, 'created_at', 'DESC']]
});
Такой запрос выдает админа и все его последние входы. Как ограничить количество последних входов до 1? Пробовал такой вариант
await AdminModel.findAndCountAll({
where: whereClause,
include: [{
model: AdminLastEntrancesModel,
where: whereForChildModel,
required: true,
duplicating: false,
limit: 1,
order: [['created_at', 'DESC']],
}],
limit: limit,
offset: offset,
order: [[{model: AdminLastEntrancesModel}, 'created_at', 'DESC']]
});
Но получаю ошибку UnhandledPromiseRejectionWarning: SequelizeDatabaseError: missing FROM-clause entry for table "AdminLastEntrancesModels"