【发布时间】:2014-05-24 22:33:41
【问题描述】:
我一直在享受 v0.10 中的新 Sails 关系,但我目前最大的挑战是通过关联查找模型。如果我要填充一个手动关联,例如一组 ID,这将非常容易。但是,我似乎找不到使用 Sails 关联处理查找的正确方法。
我提供了一些示例代码,其中概述了两个模型,一个公司和一个用户。公司可以有多个用户,一个用户只能有一个公司。这是一个非常简单的一对多关系,目标是找到与用户 ID 匹配的所有公司。
## Company.js
name:
type: 'string'
required: true
users:
collection: 'User'
via: 'company'
## User.js
company:
model: 'Company'
required: true
last_name:
type: 'string'
required: true
first_name:
type: 'string'
required: true
## Lookup Users by Company ID of '2'
User.find(where: company: 2).exec(console.log)
# Result
# [] - Array of users matching that company ID
## ---- The Problem / Question ----
## Lookup Companies by User ID '1'
Company.find(where: users: contains: 1).exec(console.log)
# Result
# Error (E_UNKNOWN) :: Encountered an unexpected error:
# error: column company.users does not exist
# Details:
# { error: 'E_UNKNOWN',
# summary: 'Encountered an unexpected error',
# status: 500,
# raw: 'error: column company.users does not exist' }
如果您对处理此查找的最佳方式有任何想法,我将不胜感激!
【问题讨论】:
-
看起来您的模型定义不正确:beta.sailsjs.org/#/documentation/reference/Models
-
它们是,这与属性字面上不存在时的消息相同。
标签: node.js associations sails.js waterline