排序联接两个不相关的表 [英] Sequelize joining two tables which are not associated
问题描述
我试图通过联接两个没有使用关系关联"的表来检索数据.这两个表如下:
I am trying to retrieve data by joining two tables which are not "associated" using a relationship. These two tables are as below:
mysql> desc partner_txns;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| txn_id | int(11) | NO | | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
| txn_type | varchar(1) | YES | | NULL | |
| txn_amnt | double | YES | | NULL | |
| desc | varchar(64) | YES | | NULL | |
| createdBy | int(11) | NO | MUL | NULL | |
| created_on | datetime | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
mysql> desc accounts_master;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(64) | NO | | NULL | |
| owner | int(11) | NO | MUL | NULL | |
| type | int(11) | YES | | 0 | |
| expires_on | datetime | NO | | NULL | |
| max_lists | int(11) | YES | | 10 | |
| max_groups | int(11) | YES | | 10 | |
| createdBy | int(11) | NO | MUL | NULL | |
| modifiedBy | int(11) | NO | MUL | NULL | |
| created_on | datetime | NO | | NULL | |
| modified_on | datetime | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
在不使用sequelize的情况下,通常我会这样进行SQL查询:
Without using sequelize, normally I would make an SQL query like this:
mysql> select a.user_id, b.name from partner_txns a, accounts_master b where a.createdBy = 3 and a.user_id = b.owner;
+---------+-------------+
| user_id | name |
+---------+-------------+
| 8 | New account |
| 8 | Comviva |
| 8 | Infosys |
| 9 | HDFC |
| 9 | INTEGRA |
+---------+-------------+
5 rows in set (0.00 sec)
使用Sequelize进行此操作等效于什么,假设我将这两个表作为两个模型,如下所示:
What is the equivalent of doing this using Sequelize, assuming I have these two tables as two models as below:
- PartnerTxn
- 帐户
我很想这样使用:
var Model = require('ecp_model');
Model.PartnerTxn.findAll({
where: {createdBy:3 },
include : [{model:Model.Account, attribute:['name']}]
}).then(function(results) {
console.log("results:", results);
});
[rv.nath@localhost authserver]$
但是这将不起作用,因为这两个表没有关联.所以,我得到如下错误:
But then this won't work because these two tables are not related. So, I get an error as below:
Unhandled rejection Error: Account is not associated to PartnerTxn!
at validateIncludedElement (/var/opt/ecp_db/node_modules/sequelize/lib/model.js:569:11)
at /var/opt/ecp_db/node_modules/sequelize/lib/model.js:452:29
at Array.map (native)
at validateIncludedElements (/var/opt/ecp_db/node_modules/sequelize/lib/model.js:448:37)
at null.<anonymous> (/var/opt/ecp_db/node_modules/sequelize/lib/model.js:1360:32)
at tryCatcher (/var/opt/ecp_db/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/var/opt/ecp_db/node_modules/bluebird/js/release/promise.js:503:31)
at Promise._settlePromise (/var/opt/ecp_db/node_modules/bluebird/js/release/promise.js:560:18)
at Promise._settlePromise0 (/var/opt/ecp_db/node_modules/bluebird/js/release/promise.js:605:10)
at Promise._settlePromises (/var/opt/ecp_db/node_modules/bluebird/js/release/promise.js:684:18)
at Async._drainQueue (/var/opt/ecp_db/node_modules/bluebird/js/release/async.js:126:16)
at Async._drainQueues (/var/opt/ecp_db/node_modules/bluebird/js/release/async.js:136:10)
at Immediate.Async.drainQueues [as _onImmediate] (/var/opt/ecp_db/node_modules/bluebird/js/release/async.js:16:14)
at processImmediate [as _immediateCallback] (timers.js:383:17)
推荐答案
我知道将近一年,但万一有人遇到您并寻找正确答案的话.
I know this is almost a year afterward, but in case anyone is in your situation and looking for the correct answer.
当第三个表链接两个单独的数据类型时,您正在寻找Sequelize的BelongToMany(Through).
When you have two separate data types linked by a third table, you are looking for Sequelize's BelongToMany(Through).
因此,在您的情况下,您需要定义第三个模型:UserMaster.您将为UserMaster提供两个属性:user_id(与PartnerTxn属性相同)和所有者(与Account属性相同).
So in your case, you would need to define a third Model: UserMaster. You would give UserMaster two attributes: user_id (same as the PartnerTxn attribute) and owner (same as the Account attribute).
然后您将执行以下操作:
Then you would do this:
Account.belongsToMany(PartnerTxn, {through: 'UserMaster'});
PartnerTxn.belongsToMany(Account, {through: 'UserMaster'});
有关更多参考,请参见文档有信息.
For further reference, the documentation has information.
这篇关于排序联接两个不相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!