Sails.js关联使用不同的连接填充
我有两种型号 国家/地区-来自mysql服务器Sails.js关联使用不同的连接填充,sails.js,waterline,Sails.js,Waterline,我有两种型号 国家/地区-来自mysql服务器 Country = { tableName: 'countries', connection: 'someMysqlServer', schema: true, migrate: 'safe', autoCreatedAt: false, autoUpdatedAt: false, attributes: { country_id: { type: 'integer',
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.findOneById1.populate'country\u id'.execconsole.log
并得到错误
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连接?好吧,因为这两种模型在不同的数据库连接上,所以您无法进行实际的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之类的东西中。然后在用户查找回调中,您可以从缓存存储中按id获取国家/地区。除非您希望此数据定期更改,否则这将快得多。您可以编写一个服务,在您的其他数据库中进行延迟查找,然后从缓存开始提供服务,或者在您的应用程序启动时将它们全部提前缓存。谢谢您的回答,但如果我使用{rest:true},则此代码将自动生成。关于服务查找-我想。弹性搜索