首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sails.js关联使用不同的连接填充

Sails.js关联使用不同的连接填充
EN

Stack Overflow用户
提问于 2014-09-27 04:25:43
回答 1查看 577关注 0票数 1

我有两个型号

国家/地区-来自mysql服务器

代码语言:javascript
复制
Country = {
   tableName: 'countries',
   connection: 'someMysqlServer',

   schema: true,
   migrate: 'safe',
   autoCreatedAt: false,
   autoUpdatedAt: false,

   attributes: {
    country_id: {
        type: 'integer',
        primaryKey: true,
        autoIncrement: true
    },
    ....
  }
};

User = {
    connection: 'somePostgresqlServer',

    attributes: {
        id: {
            type: 'integer',
            primaryKey: true,
            autoIncrement: true
        },

        country_id: {
            model: 'country'
        },

$> User.findOneById(1).populate('country_id').exec(console.log)

和get错误

代码语言:javascript
复制
sails> Error (E_UNKNOWN) :: Encountered an unexpected error
: Unable to determine primary key for collection `countries` because an error was encountered acquiring the collection definition:
[TypeError: Cannot read property 'definition' of undefined]
  at _getPK (/projects/foturist-server/node_modules/sails-postgresql/lib/adapter.js:923:13)
  at StrategyPlanner.__FIND__.Cursor.$getPK (/projects/foturist-server/node_modules/sails-postgresql/lib/adapter.js:504:20)
.....

Details:  Error: Unable to determine primary key for collection `countries` because an error was encountered acquiring the collection definition:
[TypeError: Cannot read property 'definition' of undefined]

为什么国家/地区协会使用postgre连接?

EN

回答 1

Stack Overflow用户

发布于 2014-09-27 12:49:08

因为这两个模型位于不同的数据库连接上,所以您将无法进行实际的SQL连接。我想你需要的是一个

代码语言:javascript
复制
User.find({id: 1}).exec(function(user) { 
    var theUser = user;
    Country.find(user.country_id)
    .exec(function(country) {
        theUser.country = country;
        return theUser;
    }); });

我不确定您试图解决的具体需求是什么,但由于国家/地区的查找表不太可能频繁更改,并且位于完全不同的数据存储中,因此我建议将这些数据缓存到Redis或Memcache中。然后,在你的User find回调中,你可以通过id从你的缓存存储中获取国家。除非您希望此数据定期更改,否则速度会快得多。您可以编写一个服务,在您的其他数据库中执行延迟查找,然后从缓存中提供服务,或者在应用程序启动时将它们全部缓存起来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26067769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档