LoginSignup
4
2

More than 5 years have passed since last update.

Sequelizeのリレーションを図にしてみた

Last updated at Posted at 2018-04-01

はじめに

Sequelizeを使っていたがリレーションの関数の動きが分かりづらいのでUMLを用いて図にしてみた

以下の2つのテーブルを例に進める。

run.js
const TableA = this.sequelize.define('TableA');
const TableB = this.sequelize.define('TableB');

テーブル例

belongsTo

run.js
TableA.belongsTo(TableB);

tableA.getTableB();

belongsTo

hasOne

run.js
TableA.hasOne(TableB);

tableA.getTableB();

hasOne

hasMany

run.js
TableA.hasMany(TableB);

tableA.getTableBs();

hasMeny

belongsToMany

run.js
TableA.belongsToMany(TableB, {through: 'TableC'});
TableB.belongsToMany(TableA, {through: 'TableC'});

tableA.getTableBs();
tableA.setTableBs();
tableA.addTableB();
tableA.addTableBs();

tableB.getTableAs();
tableB.setTableAs();
tableB.addTableA();
tableB.addTableAs();

belongsToMany

おわりに

図式化するとなんとなくわかりやすくなった気はする。(なんとなく

わからないときは素直にAPI Docsを見よう!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2