在连接表时如何忽略序列中的特定外键?

时间:2019-05-01 14:17:51

标签: javascript node.js postgresql express sequelize.js

最近几个小时我一直在梳理头发,以解决此问题。我正在为sequelize应用程序使用express orm。 sequelize文档对我来说非常令人困惑,我一直在尝试寻找解决方案,但到目前为止没有任何解决方案。

我有三个关联

//models/device.js


device.associate = function(models) {
        device.hasMany(models.actuator_activity, {
          foreignKey: 'actuator_device_id',
          as: 'actuator_activities',
        });
        device.hasMany(models.actuator_activity, {
          foreignKey: 'sensor_device_id',
        });
        device.hasMany(models.sensor_data, {
          foreignKey: 'sensor_device_id',
          as: 'sensor_data',
        });
      };




//models/sensordata.js


sensor_data.associate = function(models) {
    sensor_data.belongsTo(models.device, {
      foreignKey: 'sensor_device_id',
      onDelete: 'CASCADE',
    });
  };




//models/actuatoractivity.js


    actuator_activity.associate = function(models) {
        actuator_activity.belongsTo(models.device, {
          foreignKey: 'actuator_device_id',
          onDelete: 'CASCADE',
        });
        actuator_activity.belongsTo(models.device, {
          foreignKey: 'sensor_device_id',
          onDelete: 'CASCADE',
        });
      };

运行此查询

device.findAll({include:[{model: sensor_data, as: 'sensor_data'}, {model: actuator_activity, as: 'actuator_activities'}]})

返回此:

{
            "id": 2,
            "name": "TEST DEVICE 2",
            "role": "SENSOR",
            "type": "SOIL MOISTURE",
            "io_pin": 5,
            "added_timestamp": "2019-05-01T12:18:08.317Z",
            "client_added_timestamp": "2019-04-23T20:44:29.597Z",
            "min_value": 0,
            "max_value": 1000,
            "sensor_data": [
                {
                    "id": 2,
                    "sensor_device_id": 2,
                    "created_timestamp": "2019-04-23T20:44:29.597Z",
                    "sensor_value": 300
                }
            ],
            "actuator_activities": [
                {
                    "id": 1,
                    "created_timestamp": "2019-04-23T20:44:29.597Z",
                    "name": "Motor turned on",
                    "actuator_device_id": 1,
                    "sensor_device_id": 2
                }
            ]
        },
        {
            "id": 1,
            "name": "TEST DEVICE 1",
            "role": "ACTUATOR",
            "type": "MOTOR",
            "io_pin": 7,
            "added_timestamp": "2019-05-01T12:17:14.026Z",
            "client_added_timestamp": "2019-04-23T20:44:29.597Z",
            "min_value": 0,
            "max_value": 1,
            "sensor_data": [],
            "actuator_activities": []
        }

但是我想要得到的是这个

{
            "id": 2,
            "name": "TEST DEVICE 2",
            "role": "SENSOR",
            "type": "SOIL MOISTURE",
            "io_pin": 5,
            "added_timestamp": "2019-05-01T12:18:08.317Z",
            "client_added_timestamp": "2019-04-23T20:44:29.597Z",
            "min_value": 0,
            "max_value": 1000,
            "sensor_data": [
                {
                    "id": 2,
                    "sensor_device_id": 2,
                    "created_timestamp": "2019-04-23T20:44:29.597Z",
                    "sensor_value": 300
                }
            ],

        },
        {
            "id": 1,
            "name": "TEST DEVICE 1",
            "role": "ACTUATOR",
            "type": "MOTOR",
            "io_pin": 7,
            "added_timestamp": "2019-05-01T12:17:14.026Z",
            "client_added_timestamp": "2019-04-23T20:44:29.597Z",
            "min_value": 0,
            "max_value": 1,
            "actuator_activities": [
                {
                    "id": 1,
                    "created_timestamp": "2019-04-23T20:44:29.597Z",
                    "name": "Motor turned on",
                    "actuator_device_id": 1,
                    "sensor_device_id": 2
                }
            ]
        }

这里的想法是将id与嵌套的actuator_device_idsensor_device_id匹配

如果我删除与actuator_activitydevices与外键sensor_device_id的关联,它可以按我期望的方式工作,但是我需要保留它,因为我需要{{1}的级联选项})。

有没有办法,例如,查询时我可以指定忽略/排除此特定外键?

如果没有,我如何在这里实现目标?

非常感谢您的帮助,感谢您阅读这篇冗长的文章。

0 个答案:

没有答案