title: sequelize association N:M Table and 1:N Table
body: <p>I am using sequelize, mysql for node.js! <br>
  Have a question and post it.<br>
  First, to create a review table with N: M relationship between user table and store table!<br>
  Please refer to the ReviewImage Table, which is a 1: N relationship with the Review Table.<br>
   For synchronization, please refer to the Review Table and ReviewImage Table.<br>
  I don't know what the problem is even if I browse the sequelize API documentation!<br>
  Have you ever done the wrong database design?<br>
  Should I design a review image table?<br></p>
  
  <pre class="lang-js prettyprint-override"><code>const Sequelize = require('sequelize');
  const env = process.env.NODE_ENV || 'development';
  const config = require('../config/config')[env];
  const db = {};
  
  const sequelize = new Sequelize(
    config.database, config.username, config.password, config);
  
  db.sequelize = sequelize;
  db.Sequelize = Sequelize;
  db.User = require('./UserModel')(sequelize, Sequelize);
  db.Store = require('./StoreModel')(sequelize, Sequelize);
  db.Review = require('./reviewModel')(sequelize, Sequelize);
  db.ImageReview = require('./imageReviewModel')(sequelize, Sequelize);
  
  /** N:M User : Store (Review) */
  db.User.belongsToMany(db.Store, { through: db.Review});
  db.Store.belongsToMany(db.User, { through: db.Review});
  
  /** 1:N review : imageReview */
  db.Review.hasMany(db.ImageReview);
  db.ImageReview.belongsTo(db.Review);
  
  module.exports = db;
  
  </code></pre>
  
  <pre><code>// userModel.js
  module.exports = (sequelize, DataTypes) =&gt; {
      return sequelize.define('User',{
          userPlatformType:{
              type: DataTypes.STRING(50),
          },
          userNickname:{
              type: DataTypes.STRING(50),
              allowNull: false,
              unique : true,
          },
          customerEmail: {
              type: DataTypes.STRING(100),
              allowNull: false,
              unique: true,
          },
          userPassword:{
              type: DataTypes.STRING(200),
              allowNull: false,
          },
          salt:{
              type: DataTypes.STRING(200),
          },
          userPhoneNumber:{
              type: DataTypes.STRING(45),
          },
          userDateOfBirth:{
              type: DataTypes.DATEONLY,
          },
          userWeddingDay:{
              type: DataTypes.DATEONLY,
          },
          userProfile: {
              type: DataTypes.STRING(200),
              allowNull: true,
          },
      }, {
          freezeTableName: true,
      });
  };
  </code></pre>
  
  <pre><code>// storeModel.js
  
  module.exports = (sequelize, DataTypes) =&gt; {
      return sequelize.define('Store',{
          storeName:{
              type: DataTypes.STRING(50),
          },
          storeCategory: {
              type: DataTypes.STRING(100),
              allowNull: false,
              unique: true,
          },
          storeAddress:{
              type: DataTypes.STRING(150),
              allowNull: false,
              unique : true,
          },
          storeLatitude:{
              type: DataTypes.STRING(45),
          },
          storeLongtitude:{
              type: DataTypes.STRING(45),
          },
          storeCondition: {
              type: DataTypes.STRING(200),
              allowNull: false,
          },
          storePhoneNumber:{
              type: DataTypes.STRING(200),
              allowNull: false,
          },
          storeProfile:{
              type: DataTypes.STRING(200),
          },
          storeWifi:{
              type: DataTypes.STRING(45),
          },
          storePost:{
              type: DataTypes.STRING(100),
          },
          storeItem:{
              type: DataTypes.STRING(100),
          },
          storeIntro:{
              type: DataTypes.STRING(100),
          },
          storeLike: {
              type: DataTypes.INTEGER,
              defaultValue: 0,
          },
          storeRating: {
              type: DataTypes.INTEGER,
              defaultValue: 0,
          },
          storeIsJoin: {
              type: DataTypes.BOOLEAN,
              defaultValue: false,
          },
      }, {
          freezeTableName: true,
      });
  };
  </code></pre>
  
  <pre><code>//ReviewModel.js
  module.exports = (sequelize, DataTypes) =&gt; {
      return sequelize.define('Review',{
          reviewWriter :{
              type: DataTypes.STRING(50),
              allowNull: false,
          },
          reviewContent :{
              type: DataTypes.STRING(200),
              allowNull: false,
          },
          reviewGrade : {
              type: DataTypes.INTEGER,
              allowNull: false,
          },
          reviewIsDeclare : {
              type: DataTypes.BOOLEAN,
              allowNull: false,
          },
      }, {
          freezeTableName: true,
      });
  };
  </code></pre>
  
  <pre><code>//ReviewImage.js
  module.exports = (sequelize, DataTypes) =&gt; {
      return sequelize.define('ImageReview',{
          reviewImageUrl :{
              type: DataTypes.STRING(200),
              allowNull: false,
          },
      }, {
          freezeTableName: true,
      });
  };
  </code></pre>