- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试通过连接两个未使用关系“关联”的表来检索数据。这两张表如下:
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 查询:
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 执行此操作的等效项是什么:
我很想像这样使用:
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]$
但是这将不起作用,因为这两个表不相关。所以,我收到如下错误:
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)
最佳答案
我知道这已经快一年了,但以防万一有人遇到你的情况并正在寻找正确的答案。
当您有两个单独的数据类型由第三个表链接时,您正在寻找 Sequelize 的 BelongToMany(Through)。
因此,在您的情况下,您需要定义第三个模型:UserMaster。您可以为 UserMaster 提供两个属性:user_id(与 PartnerTxn 属性相同)和 owner(与 Account 属性相同)。
然后你会这样做:
Account.belongsToMany(PartnerTxn, {through: 'UserMaster'});
PartnerTxn.belongsToMany(Account, {through: 'UserMaster'});
要进一步引用,documentation有信息。
关于mysql - Sequelize连接两个不相关的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35789512/
我正在关注 Sequelize 纪录片。在 Model Definition 部分的底部,我想知道 sequelize 变量用于什么?我可以删除它吗? { sequelize, modelNa
我有这个原始 SQL 查询。 SELECT restaurants.name AS restaurant, ROUND((6371 * ACOS(COS(RADIANS(6.9271)
我有一个 postgres 数据库,我正在使用 Sequelize。从表 车辆 和 预订 我试图在给定开始日期和结束日期的情况下获得所有可用车辆。 如果我使用给定日期搜索 Bookings: Book
我正在尝试使用 HapiJS 和 Sequelize 开始一个项目,并且开始时,希望在没有 Sequelize CLI 的情况下使事情正常工作,以了解一切是如何结合在一起的。 我见过多个示例项目,例如
sequelize init:models 命令会创建一个“models”文件夹并添加一个 index.js。 有人能解释一下这个 index.js 文件到底做了什么,以及它如何适应 Sequeliz
查看此 Sequelize tutorial 中的 models/blog.js 定义. module.exports = (sequelize, type) => { return sequ
我收到一个错误 No overload matches this call 每当我尝试使用 @HasMany() 或 @BelongsTo 装饰器时。 我正在使用 nestjs/sequelize:
const gamem = await gamemodes.findAll({attributes: ['name']}); const newme = await gamemodes.fin
我们如何使用 sequelize 4.2 创建由所有模型应用/继承的自定义实例方法?在 Sequelize 3 中,我们曾经在扩展到所有其他模型的主模型的“定义”部分中有一个“instanceMeth
我正在使用 Typescript、NodeJS 和sequelize-auto-ts 进行模型生成。我想要放置包含查找查询的两个模型给了我错误的结果,这又是由于触发了错误的查询。这是有问题的两个模型;
假设我在用户表中有 5 列 name email state isOnline createdAt | N
我有两张表,其中一张表具有另一张表的 ID。 1:1 关系。 所以像 EventFeedback somePrimaryKey userEventID UserEvent us
如何使用对象创建具有现有关联的条目? 例如, User.create({ socialMedia: [{ socialId: 1, //should reference existing
如何创建在创建 Sequelize 模型的新实例时以编程方式生成的默认值?我已经在某处阅读了如何执行此操作,但在任何地方都找不到。我以为这与classMethods有关,但我不知道应该调用什么方法。
当我设置关联时,有没有办法使用 Sequelize (sequelizejs.com) 输出所有在对象上神奇创建的函数。 例如;我有一个用户模型,我设置 User.belongsToMany(User
我该如何制作 Music是`音乐? { id: 4 name: "playlist 1" created_at: "2015-04-21T21:43:07.000Z" updated_
我可以使用 sequelize 从可用模型创建数据库模式吗?我在一个缺少许多迁移且没有测试的项目上工作。要运行测试,我需要创建一个新的 db (sqlite3),但我无法使用迁移初始化其架构(因为它们
我有一个与其他模型相关联的简单 Sequelize 模型。 module.exports = function (sequelize, DataTypes) { var Votes = seque
我有一个像这样设置的协会: m.User.hasMany(m.Interests, { joinTableName: 'user_interests', foreignKey: 'user_id' }
我有以下型号: var User = Seq.define('user', { firstName: { type: Sequelize.STRING,
我是一名优秀的程序员,十分优秀!