续集,关联返回时出现问题
我目前正在尝试 Sequelize 并有两个对象,一个 Person
和 Position
,当获取人员列表时我想获得他们的位置。
模型:
var User = sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
var Position = sequelize.define('position', {
name: Sequelize.STRING,
affiliation: Sequelize.STRING
});
Position.hasMany(User, { foreignKey : 'position_id' });
User.belongsTo(Position, { foreignKey : 'position_id'});
我的查询:
User.findAll({ fetchAssociations: true }, function(results) {
//I've tried doing some work in here, but haven't found the correct procedure.
}).on('success', function(results) {
res.render('users', {
title: 'user page',
users: results
});
});
查看日志它根本不会查询 Person
。我需要使用查询链吗?从文档中我发现它似乎应该自动获取关联。
I'm currently experimenting with Sequelize and have two objects, a Person
and Position
, When getting a list of persons I want to get their position.
models:
var User = sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
});
var Position = sequelize.define('position', {
name: Sequelize.STRING,
affiliation: Sequelize.STRING
});
Position.hasMany(User, { foreignKey : 'position_id' });
User.belongsTo(Position, { foreignKey : 'position_id'});
My query:
User.findAll({ fetchAssociations: true }, function(results) {
//I've tried doing some work in here, but haven't found the correct procedure.
}).on('success', function(results) {
res.render('users', {
title: 'user page',
users: results
});
});
Watching the log it never queries Person
at all. Do I need to use queryChaining? From the documentation I was able to find it appeared it should auto fetch associations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 2013 年 4 月的 v1.7.0 开始,您只需将关联扩展到:
然后查找具有关联职位的所有用户
From April 2013 in v1.7.0 you need just to expand your Associations to:
and then find all Users with associated Positions
好的,这里是如何解决您的问题的完整示例(是的,这很糟糕):
这将首先获取所有用户,然后获取每个用户的位置并将其保存在 _users 数组中。然后它渲染页面。它没有经过测试,但应该可以解决问题。
Ok here a complete example how to fix your problem (and yeah that's crap):
This will first get all the users, then get the position of each user and save them in the _users-array. Afterwards it renders the page. It's not tested but should do the trick.
由于我重写了 1.0 的sequelize,因此我删除了对 fetchAssociations 的支持。我将在接下来的几周内添加它。但是让我们看一下您的代码:
您正在使用类似下划线的列名称,因此您可能希望对自动生成的列使用下划线选项: http://www.sequelizejs.com/?active=usage#usage -->向下滚动一点或搜索下划线。这可能会为您节省belongsTo/hasMany 的选项。
你想用 findAll 做的事情是:就像这样:
正如我刚刚注意到的......获取所有关联的对象有点棘手:D 现在就这么多。希望有帮助。
since I rewrote sequelize for 1.0 I removed the support of fetchAssociations. I will add it in the next weeks. But let's take a look at your code:
You are using underscore-like column names, so you probably want to use the underscore option for automatically generated columns: http://www.sequelizejs.com/?active=usage#usage --> scroll a bit down or search for underscore. This might save you the options for belongsTo/hasMany.
What you want to do with findAll is smth. like this:
And as I just noticed... it's somewhat tricky to get all associated objects :D So much for now. Hope that helped.
这允许序列化 json 以用于模型的任何操作。阅读它,很好,我认为这是最好的方法
sequelize-virtual-fields
This allow serialize a json for anywhere action about a model. Read it, very well, i think that is the best way
sequelize-virtual-fields