【问题标题】:Node.js Sequelize object "not associated" when it isNode.js Sequelize 对象“未关联”,当它是
【发布时间】:2017-02-01 08:02:15
【问题描述】:

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

我建立了关联,唯一的问题是我得到了一个

错误:录音未与接收器关联!

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

定义

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

【问题讨论】:

    标签: node.js postgresql orm sequelize.js


    【解决方案1】:

    这可能已经过时了,但错误是“正确的”,receiver 和 recording 没有直接关联,您尝试直接获取接收器实例,但您定义了join table 称为 receiverRelation,因此您需要查询如下对象:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-27
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多