我有两个型号
国家/地区-来自mysql服务器
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错误
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连接?
发布于 2014-09-27 12:49:08
因为这两个模型位于不同的数据库连接上,所以您将无法进行实际的SQL连接。我想你需要的是一个
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从你的缓存存储中获取国家。除非您希望此数据定期更改,否则速度会快得多。您可以编写一个服务,在您的其他数据库中执行延迟查找,然后从缓存中提供服务,或者在应用程序启动时将它们全部缓存起来。
https://stackoverflow.com/questions/26067769
复制相似问题