2015-05-07 49 views
1

我想在Node.js的Sequelize中編寫一個belongsToMany關聯模型。我正嘗試將接收設備與將要播放的錄音關聯起來。一個錄音將在許多接收設備上播放。我創建了一個直通表,其中關聯存儲在確保關聯類型爲「音頻」的(範圍)子句中,因爲還會有其他表使用此關聯。Node.js Sequelize對象「不相關」,當它是

我建立的關聯,唯一的問題是我收到的

Error: recording is not associated to receiver!

錯誤。我不知道我錯了哪裏,但據我所知,他們是,當我定義它們時,肯定是

定義

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

查詢代碼

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

回答

0

這可能是相當出來的時候,但這個錯誤是 '正確的',receiverrecording直接相關,您正試圖直接獲取接收器實例,但您定義了一個join table被稱爲receiverRelation,所以你需要查詢像的對象:

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

我敢肯定,你幾乎在兩年前解決了這個問題,但我也想幫助