簡體   English   中英

Sequelize MySQL - 連接同一個關聯表中的兩行

[英]Sequelize MySQL - Join with two rows from same associated table

我遇到了一個涉及 Sequelize MySQL 的棘手問題。 我有兩個模型: CalibrationDevice ,分別關聯多對一。

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
    });

現在的重點是我想查詢一個Device列表,加入Calibration ,每個設備項都有兩個日期屬性,引用兩個關聯的校准,一個是最近的日期,另一個是未來最近的日期。 這非常棘手,我還沒有設法找到解決方案。 非常感謝您的幫助。

更新:這是我目前正在做的,事實上,我對此並不滿意,因為我更喜歡另一種方式將它全部組合在 1 個查詢中,而不是像這樣的 3 個:

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] };

我認為可以通過以下方式實現關聯:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM