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

标签 node.js postgresql orm sequelize.js

我正在尝试在 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/

相关文章:

json - 我想得到一个 json 格式的 Neo4j 结果集?

javascript - 使用 Node 集群不断地与套接字连接进行连接/断开连接

javascript - 如何测试我已发布的 NPM 包的本地安装?

node.js - 在 Sequelize 中使用迁移添加具有外键约束的表列

python - 惰性评估是如何实现的(例如在 ORM 中)

python - SQLAlchemy ORM : modify WHERE clause

node.js - Nodejs - 错误回调的重新调用函数 - 是否有非阻塞方式?

ruby-on-rails - 在 Postgres 中使用子查询进行 Rails 分页查询

SQL 连接两个表中任一表中的空值

orm - 使用工厂进行映射和重构的域模型快照