首页 文章

Node.js Sequelize对象“not associated”

提问于
浏览
1

我正在尝试在Node.js中的Sequelize中编写belongsToMany关联模型 . 我正在尝试将接收器设备与将要播放的录音相关联 . 一个录音将在许多接收器设备上播放 . 我创建了一个直通表,其中存储关联,其中(范围)子句确保关联类型是“音频”,因为还有其他表使用此关联 .

我 Build 了协会,唯一的问题是我得到了一个

错误:录音与接收器无关!

错误 . 我不确定我哪里出错了,但据我所知,当我定义它们时它们肯定是相关的 .

Definition

var receiver = sequelize.define( 'receiver', {
    'id': {
        type: Sequelize.BIGINT,
        field: 'receiver_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'organizationID': {
        type: Sequelize.BIGINT,
        field: 'organization_id',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'identifier': {
        type: Sequelize.STRING( 64 ),
        field: 'identifier',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'secret' : {
        type:  Sequelize.STRING( 64 ),
        field: 'secret',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'iterations' : {
        type:  Sequelize.INTEGER,
        field: 'iterations',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'sodium': {
        type: Sequelize.STRING( 64 ),
        field: 'sodium',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'algorithm' : {
        type:  Sequelize.STRING( 8 ),
        field: 'algorithm',
        allowNull: false,
        defaultValue: 'sha256'
    },
    'name' : {
        type:  Sequelize.STRING( 256 ),
        field: 'name',
        allowNull: false
    }
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'deletedAt' : 'deleted',
    'tableName' : 'receivers',
    'paranoid'  : true
} );

var receiverRelation = sequelize.define( 'receiverRelation', {
    'receiverID': {
        type: Sequelize.BIGINT,
        field: 'receiver_id',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'relationID': {
        type: Sequelize.BIGINT,
        field: 'relation_id',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'type': {
        type: Sequelize.STRING( 8 ),
        field: 'type',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    }
}, {
    'timestamps': false,
    'tableName' : 'receiver_relations'
} );

var recording = sequelize.define( 'recording', {
    'id': {
        type: Sequelize.BIGINT,
        field: 'recording_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'receivingComplete' : {
        type:  Sequelize.BOOLEAN,
        field: 'receiving_complete',
        allowNull: false,
        defaultValue: false
    },
    'sendingComplete' : {
        type:  Sequelize.BOOLEAN,
        field: 'sending_complete',
        allowNull: false,
        defaultValue: false
    },
    'bitrate' : {
        type:  Sequelize.INTEGER,
        field: 'bitrate',
        allowNull: false,
        defaultValue: false
    },
    'samplerate' : {
        type:  Sequelize.INTEGER,
        field: 'samplerate',
        allowNull: false,
        defaultValue: false
    },
    'channels' : {
        type:  Sequelize.INTEGER,
        field: 'channels',
        allowNull: false,
        defaultValue: false
    },
    'format' : {
        type:  Sequelize.STRING( 8 ),
        field: 'format',
        allowNull: false,
        defaultValue: false
    }
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'deletedAt' : 'deleted',
    'tableName' : 'recordings',
    'paranoid'  : true
} );
// Recordings to receivers
receiver.belongsToMany( recording,{
        'through' : {
                'model' : receiverRelation,
                'scope' : {
                            'type' : 'audio'
                        }
                },
                'foreignKey' : 'receiver_id',
                'as' : 'recording'
} );
recording.belongsToMany( receiver, {
            'through' : {
                    'model' : receiverRelation,
                    'scope' : {
                            'type' : 'audio'
                    }
            },
            'foreignKey' : 'relation_id',
            'as': 'receiver'
} );

Query Code

db.receiver.findAll( {
    'include' : [ {
        'model' : db.recording,
        'where' : { 'id' : recordingRow.get( 'id' ) }
    } ]
} )

1 回答

  • 0

    这可能是非常时间的,但错误是'correct', receiverrecording 不是 directly 关联,您试图直接获取接收器实例,但是您定义了一个名为 receiverRelationjoin table ,因此您需要查询对象,如:

    db.receiver.findAll({
      'include' : [{
        'model' : db.recording,
        'through': {'where' : { 'recording_id' : recordingRow.get( 'id' ) } }
      }]
    })
    

    我很确定你差不多两年前解决了这个问题,但我想帮忙

相关问题