Typography

活版印字


  • Home
  • Archive
  • Categories
  • Tags
  •  

© 2020 alincode

Theme Typography by Makito

Proudly published with Hexo

Sequelize belongsToMany

Posted at 2016-07-03 Sequelize 

當同個 table 有兩個同樣的關聯名稱的時候,需要將其中一個設別名,使用方式如下,網路上的資料不太多,文件也不太容易找,所以特別在此記錄一下。

GameList.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = {
attributes: {
// 略
},
associations: function() {
GameList.belongsTo(User);
GameList.belongsToMany(User, {
through: 'game_list_userlike',
as: 'UserLike'
});

},
options: {
tableName: 'game_list',
}
};

User.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
module.exports = {
attributes: {
// 略
},
associations: function() {
User.hasMany(GameList);
User.belongsToMany(GameList, {
through: 'game_list_userlike',
as: 'UserLike'
});

},
options: {
tableName: 'user',
}
};

取值方式

1
2
3
4
5
6
7
let option = {
include: [{
model: User,
as: 'UserLike'
}]
};
let gamelists = await GameList.findAll(option);

Share 

 Previous post: lodash 常見功能(1) Next post: 單元測試錯誤排解 (1) 

© 2020 alincode

Theme Typography by Makito

Proudly published with Hexo