多个一对多关系建模

时间:2013-05-10 04:03:03

标签: sequelize.js

我有2个模型,这两个模型都与用户表有关系,即项目用户拥有用户的外键,评论也属于用户

项目用户模型(projectusers.js文件):

var User = require('./users').table;
var pusers_table = db.define('project_users', { ... });
pusers_table.belongsTo(User);
User.hasMany(pusers_table);

用于评论模型(comments.js文件):

var User = require('./users').table;
var comments_table = db.define('comments', { ... });
comments_table.belongsTo(User);
User.hasMany(comments_table);

请注意,所有三个(用户,项目用户和评论)都在不同的JS文件中定义,并使用requirejs包含。

  TypeError: Cannot read property 'associationType' of null
        at null.<anonymous> (./node_modules/sequelize/lib/dialects/abstract/query.js:328:54)
        at Array.forEach (native)
        at module.exports.buildAssociatedDaoInstances (./node_modules/sequelize/lib/dialects/abstract/query.js:324:21)
        at module.exports.transformRowWithEagerLoadingIntoDao (./node_modules/sequelize/lib/dialects/abstract/query.js:295:37)
        at null.<anonymous> (./node_modules/sequelize/lib/dialects/abstract/query.js:282:50)
        at Array.map (native)
        at module.exports.transformRowsWithEagerLoadingIntoDaos (./node_modules/sequelize/lib/dialects/abstract/query.js:281:58)
        at module.exports.handleSelectQuery (./node_modules/sequelize/lib/dialects/abstract/query.js:262:54)
        at module.exports.AbstractQuery.formatResults (./node_modules/sequelize/lib/dialects/abstract/query.js:68:34)
        at null.<anonymous> (./node_modules/sequelize/lib/dialects/mysql/query.js:32:35)
        at Query.Sequence.end (./node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
        at Query._handleFinalResultPacket (./node_modules/mysql/lib/protocol/sequences/Query.js:143:8)
        at Query.EofPacket (./node_modules/mysql/lib/protocol/sequences/Query.js:127:8)
        at Protocol._parsePacket (./node_modules/mysql/lib/protocol/Protocol.js:172:24)
        at Parser.write (./node_modules/mysql/lib/protocol/Parser.js:62:12)
        at Protocol.write (./node_modules/mysql/lib/protocol/Protocol.js:37:16)
        at Socket.ondata (stream.js:51:26)
        at Socket.EventEmitter.emit (events.js:117:20)
        at Socket.<anonymous> (_stream_readable.js:710:14)
        at Socket.EventEmitter.emit (events.js:92:17)
        at emitReadable_ (_stream_readable.js:382:10)
        at emitReadable (_stream_readable.js:378:5)
        at readableAddChunk (_stream_readable.js:143:7)
        at Socket.Readable.push (_stream_readable.js:113:10)
        at TCP.onread (net.js:511:21)

我刚尝试升级sequelize,现在在sequelize@1.7.0-alpha1上,但我仍有问题。

请注意,如果我只定义其中一个,我就不会收到错误(例如,如果删除评论中的关系,项目用户工作完全正常,反之亦然)。

我做错了什么?

0 个答案:

没有答案