sequelize.js:hasMany - belongsTo索引重复

时间:2014-11-25 19:33:11

标签: associations sequelize.js

我们说我有两个模型 - UserFolder。用户可能有许多文件夹,文件夹可能只属于一个用户 - 它的所有者。

User.hasMany Folder,
  as : "Folders"
Folder.belongsTo User,
  as : "Owner"

结果我在Folder模型中获得了索引加倍:

\d+ "Folders"
  Column   |           Type           |                       Modifiers                        | Storage  | Stats target | Description
-----------+--------------------------+--------------------------------------------------------+----------+--------------+-------------
 id        | integer                  | not null default nextval('"Folders_id_seq"'::regclass) | plain    |              |
 createdAt | timestamp with time zone | not null                                               | plain    |              |
 updatedAt | timestamp with time zone | not null                                               | plain    |              |
 UserId    | integer                  |                                                        | plain    |              |
 OwnerId   | integer                  |                                                        | plain    |              |

我需要从双方都可以访问这种关系:

user.getFolders.sucesss (folders)->
  # folders — array of user's folders
folder.getOwner.sucesss (user)->
  # user — user object

但不是这样,我得到索引字段翻倍。似乎字段UserIdOwnerId由同一关联创建。怎么避免这个?

" sequelize":" ^ 2.0.0-rc2"

谢谢。

1 个答案:

答案 0 :(得分:0)

foreignKey属性就行了。