这适用于 sails-disk 适配器,但不适用于 sails-mongo 或 sails-postgresql。
我有 2 个模型,Feed 和 Event,其中 Feed 可以属于许多 Event。
当我创建或保存与一个或多个事件关联的新摘要记录时(通过“posts”属性),“posts”属性不会保存。但是,当我创建或保存与一个 Feed 关联的新事件(通过“已发布”属性)时,“已发布”属性会正确保存。问题:我无法从 Feed 端查询 Feed 及其事件(使用 Sails 的 .populate
),只能从事件端查询。
我可以确认传递给 Feed.create
的数据包含“posts”属性,而回调的响应则不包含“posts”属性。
sails.log.debug('Before Feed.create', data);
Before Feed.create { id: 64988,
username: 'anevening',
displayName: 'An Evening',
coverImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
profileImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
description: undefined,
links: [],
source:
{ domain: 'dola.com',
id: 64988,
url: 'http://www.dola.com/artists/an-evening' },
posts: [ 3055349, 3062549, 2866105, 2866107 ] }
Feed.create(data).exec(sails.log.debug);
{ username: 'anevening',
displayName: 'An Evening',
coverImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
profileImage: 'http://res.cloudinary.com/dostuff-media/image/upload/v1387144880/metro-bkg_1-30.png',
description: null,
links: [],
source:
{ domain: 'dola.com',
id: 64988,
url: 'http://www.dola.com/artists/an-evening' },
createdAt: Sat Nov 22 2014 14:53:21 GMT-0800 (PST),
updatedAt: Sat Nov 22 2014 14:53:21 GMT-0800 (PST),
id: '64988' }
这是我的模型的样子:
Feed.js:
module.exports = {
attributes: {
id: {
type: 'integer',
unique: true,
primaryKey: true
},
...
posts: {
collection: 'event',
via: 'posted'
}
...
}
};
Event.js:
module.exports = {
attributes: {
id: {
type: 'integer',
unique: true,
primaryKey: true
},
...
posted: {
model: 'feed'
}
...
}
};
最后,这是我的 config/models.js 和 config/connections.js 的样子:
连接:{
'default': 'mongo',
localDiskDb: {
adapter: 'sails-disk'
},
mongo: {
adapter: 'sails-mongo',
url: process.env.MONGO_HOST',
schema: true
},
postgres: {
adapter: 'sails-postgresql',
url: process.env.POSTGRES_HOST,
schema: true,
ssl: true
}
},
models: {
connection: 'mongo',
migrate: 'alter'
}
最佳答案
.create
方法不会在回调中返回填充的数据。如果您查询 Feed 模型并填充 posts
关联,您应该会看到数据。
Feed.find()
.populate('posts')
.exec(console.log);
关于node.js - Sails.js 关联 : "Many" side of one-to-many associations not saving to MongoDB or Postgres (vs. 磁盘数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27110498/