我在Node.js网络应用中使用Sequelize。我有一个多对多的关系,并使用查找表。生成的SQL看起来很好,但是当我使用它时,只会显示“group”表中的值。
这就是我设置模型的方式:
//groups
m.User.hasMany(m.Group, { as: 'UserGroups', joinTableName: 'group_users', foreignKey: 'user_id' });
m.Group.hasMany(m.User, { as: 'UserGroups', joinTableName: 'group_users', foreignKey: 'group_id' });
这是由sequelize运行的原始sql:
SELECT `group`.`id`, `group_name`, `created_date`, `description`, `rights`, `join_date` FROM `group`, `group_users` WHERE `group_users`.`user_id`=1 AND `group_users`.`group_id`=`group`.`id`;
这是我的代码:
user.getUserGroups({ attributes : [ "group.id", "group_name", "created_date", "description", "rights", "join_date" ] }).success(function (groups) {
content.groups = groups;
console.log(groups);
});
这就是控制台.log出来了:
[ { dataValues:
{ id: 16,
group_name: 'chris\'s group',
created_date: Sun Dec 16 2012 17:36:46 GMT+0000 (GMT),
description: 'test test test',
rights: 4,
join_date: Tue Oct 15 2013 13:49:05 GMT+0100 (BST) },
__options:
{ timestamps: false,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
touchedAt: 'touchedAt',
instanceMethods: [Object],
classMethods: [Object],
validate: {},
freezeTableName: true,
underscored: true,
syncOnAssociation: true,
paranoid: false,
whereCollection: [Object],
schema: null,
schemaDelimiter: '',
language: 'en',
defaultScope: null,
scopes: null,
foreignKeyConstraint: true
....
这是视图中呈现的原始JSON:
[{"id":16,"group_name":"chris's group","created_date":"2012-12-16T17:36:46.000Z","description":"test test test"},
为什么我不能看到“权利:4”被渲染,因为这肯定会被带回来?
答案 0 :(得分:1)
您可以检查您是否在续集模型定义中定义了rights
,因为toJSON()
函数根据模型定义中定义的列名进行转换。