Reputation: 3352
I'm trying to write a belongsToMany association model in Sequelize in Node.js. I'm trying to associate a receiver device with the recordings that it will be playing. One recording will be played on many receiver devices. I created a through table where the association is stored with the one where (scope) clause of making sure the type of association is for "audio" since there will be other tables using this association as well.
I set up the association, the only problem is I'm getting an
Error: recording is not associated to receiver!
error. I'm not sure where I went wrong, but as far as I can tell, they are definitely related when I defined them.
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' ) }
} ]
} )
Upvotes: 1
Views: 342
Reputation: 407
This might be pretty out of time, but the error is 'correct', receiver
and recording
are not directly associated, you are trying to fetch receiver instances directly, but you defined a join table
called receiverRelation
, so you need to query the objects like:
db.receiver.findAll({
'include' : [{
'model' : db.recording,
'through': {'where' : { 'recording_id' : recordingRow.get( 'id' ) } }
}]
})
I'm pretty sure you solved this problem almost two years ago but well I wanted to help
Upvotes: 0