gpt4 book ai didi

Node.js Sequelize object "not associated"当它是

转载 作者:行者123 更新时间:2023-11-29 13:56:48 25 4
gpt4 key购买 nike

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

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

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

最佳答案

这可能已经过时了,但错误是“正确的”,receiverrecording 不是直接关联的,您是试图直接获取接收器实例,但您定义了一个名为 receiverRelationjoin table,因此您需要像这样查询对象:

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

我很确定你在大约两年前就解决了这个问题,但我很想帮忙

关于Node.js Sequelize object "not associated"当它是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30111792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com