【发布时间】:2015-11-15 07:53:10
【问题描述】:
场景
一个用户可以有很多标签对象。一个 Tag 对象属于一个用户。一个标签有许多事务。一个事务属于一个标签。用户有很多交易。一个事务可以有多个用户。
型号
var User = sequelize.define('User', {
id: {
type: Sequelize.BIGINT,
autoIncrement: true,
primaryKey: true
},
...
}, { timestamps: false, freezeTableName: true, tableName: 'register'});
var Tag = sequelize.define('Tag', {
tagId: {
type: Sequelize.STRING(50),
primaryKey: true,
allowNull: false
},
...
}, { timestamps: false, freezeTableName: true, tableName: 'tag'});
var Transaction = sequelize.define('Transaction', {
id: {
type: Sequelize.BIGINT,
autoIncrement: true,
primaryKey: true
},
active: {
type: Sequelize.BOOLEAN,
defaultValue: true
}
}, { timestamps: false, freezeTableName: true, tableName: 'transaction'});
var UserTx = sequelize.define('UserTx', {
id: {
type: Sequelize.BIGINT,
autoIncrement: true,
primaryKey: true
}
},
{ timestamps: false, freezeTableName: true, tableName: 'user_transaction'});
关系
User.hasMany(Tag, {foreignKey: 'owner_id', foreignKeyConstraint: true});
Tag.belongsTo(User, {foreignKey: 'owner_id', foreignKeyConstraint: true});
Tag.hasMany(Transaction, {foreignKey: 'tag_id', foreignKeyConstraint: true});
Transaction.belongsTo(Tag, {foreignKey: 'tag_id', foreignKeyConstraint: true});
User.belongsToMany(Transaction, {through: {model: UserTx, unique: false}, foreignKey: 'user_id'});
Transaction.belongsToMany(User, {through: {model: UserTx, unique: false}, foreignKey: 'tx_id'});
问题
我正在尝试返回给定用户拥有的 Tag 对象列表,以及用户与其关联的 Transactions 的 Tag 对象。在纯 SQL 中:
select * from tag
left outer join transaction on tag."tagId" = transaction.tag_id
left outer join user_transaction on transaction.id = user_transaction.tx_id
where tag.owner_id = ? or user_transaction.user_id = ?
我当前的 Sequelize 查询:
Tag.findAll({
where: { owner_id: userId }, // missing OR user_transaction.user_id = userId
include: [{
model: Transaction,
attributes: ['id'],
through: {model: UserTx, where: {user_id: userId}, attributes: ['user_id', 'tx_id']},
where: {
active: true
},
required: false, // include Tags that do not have an associated Transaction
}]
})
调用此查询时,我收到以下错误:
Unhandled rejection TypeError: Cannot call method 'replace' of undefined
at Object.module.exports.removeTicks (/site/services/node_modules/sequelize/lib/utils.js:343:14)
at Object.module.exports.addTicks (/site/services/node_modules/sequelize/lib/utils.js:339:29)
at Object.QueryGenerator.quoteIdentifier (/site/services/node_modules/sequelize/lib/dialects/postgres/query-generator.js:843:20)
at generateJoinQueries (/site/services/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1207:72)
at Object.<anonymous> (/site/services/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1388:27)
at Array.forEach (native)
at Object.QueryGenerator.selectQuery (/site/services/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1387:10)
at QueryInterface.select (/site/services/node_modules/sequelize/lib/query-interface.js:679:25)
at null.<anonymous> (/site/services/node_modules/sequelize/lib/model.js:1386:32)
在 removeTicks 函数中设置断点并在“s”(列名属性)上设置监视,我注意到以下内容:
s = "Transactions"
s = "Transactions.id"
s = "Transactions.undefined" // should be Transactions.UserTx ?
s = "user_id"
s = "Transactions.undefined.user_id"
s = "Transactions.undefined"
s = "tx_id"
s = "Transactions.undefined.tx_id"
我对 N:M 的使用不正确吗?我在其他地方的“查找”查询中成功使用了“通过”构造,但是由于这个“通过”嵌套在包含中,它的行为似乎有所不同(例如要求我明确地通过.model)
任何帮助将不胜感激!
【问题讨论】:
标签: sequelize.js