简体   繁体   中英

Sequelize MySQL - Join with two rows from same associated table

I am into a tricky question involving Sequelize MySQL. I have two models: Calibration and Device , associated many-to-one respectively.

const Calibration = sequelize.define('Calibration', {
        id: {
            type: DataTypes.UUID,
            primaryKey: true,
            defaultValue: DataTypes.UUIDV4
        },
        date: {
            type: DataTypes.DATEONLY,
            allowNull: false
        },
        document: DataTypes.STRING,
        status: DataTypes.STRING
    });

    Calibration.associate = (models) => {
        models.Calibration.belongsTo(models.Device, {
            foreignKey: 'deviceId',
            onDelete: 'CASCADE'
        });
    };


const Device = sequelize.define('Device', {
        id: {
            type: DataTypes.STRING,
            primaryKey: true,
            defaultValue: DataTypes.UUIDV4
        },
        name: DataTypes.STRING,
        description: DataTypes.STRING,
        serialNo: {
            type: DataTypes.STRING,
            allowNull: false,
            unique: true
        },
        calibrationPeriod: DataTypes.INTEGER,
        location: DataTypes.STRING,
        seller: DataTypes.STRING,
        servicePartner: DataTypes.STRING,
        deviceFunction: DataTypes.STRING,
        quantity: DataTypes.INTEGER,
        status: DataTypes.STRING,
        comment: DataTypes.STRING
    });

Now the point is that I want to query a list of Device , joined with Calibration , each device item has two date attributes referencing two associated calibrations, one with most recent date and the other with the nearest date in future. It has been quite tricky and I haven't managed to find out solution. Thank you very much for your help.

Updated: This is my current doing, in fact, I'm not satisfied with this yet, as I prefer another way of combining it all in 1 query only, instead of 3 like this:

let result = await Device.findAll({
                    include: [{ model: Category, attributes: ['id', 'name'] }],
                    order: [['createdAt', 'DESC']], raw: true
                });
await Promise.map(result, async (item, i) => {
    const lastCalibration = await Calibration.findAll({
                        attributes: ['id', 'date'],
                        where: { date: { [Op.lte]: Sequelize.fn('curdate') }, deviceId: item.id },
                        include: [{ model: Device, attributes: ['id'] }],
                        order: [['date', 'DESC']],
                        limit: 1,
                        raw: true
                    });
    const nextCalibration = await Calibration.findAll({
                        attributes: ['id', 'date'],
                        where: { date: { [Op.gt]: Sequelize.fn('curdate') }, deviceId: item.id },
                        include: [{ model: Device, attributes: ['id'] }],
                        order: [['date', 'ASC']],
                        limit: 1,
                        raw: true
                    });
result[i] = { ...item, lastCalibration: lastCalibration[0], nextCalibration: nextCalibration[0] };

I think the associations can be achieved through:

include: [Calibration], 
through: { 
    where: {
      [Op.or]: [
      { date1: '10-12-2020' },
      { date2: '11-12-2020' }
    ]
   }
 },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM