Node.js Sequelize object" not associated"几时

时间:2015-05-07 21:15:01

标签: node.js postgresql orm sequelize.js

我试图在Node.js中的Sequelize中编写belongsToMany关联模型。我试图将接收器设备与它将要播放的录音相关联。一个录音将在许多接收器设备上播放。我创建了一个直通表,其中存储了关联,其中(范围)子句确保了关联的类型是" audio"因为也会有其他表使用这种关联。

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

  

错误:录音与收件人无关!

错误。我不确定自己哪里出错,但据我所知,当我定义它们时,它们绝对相关。

定义

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

1 个答案:

答案 0 :(得分:0)

这可能已经过时了,但错误是正确的',receiverrecording并非直接关联,您正在尝试直接获取接收器实例,但是您定义了一个名为join table的{​​{1}},因此您需要查询对象,如:

receiverRelation

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