官术网_书友最值得收藏!

  • Node.js 6.x Blueprints
  • Fernando Monteiro
  • 258字
  • 2021-07-14 10:35:06

Creating Band schema

Let's create the schema that will store in the database the data of each band that the user creates in the system.

  1. Open terminal/shell and type the following command:
     sequelize model:create --name Band --attributes "name:string, description:string, album:string, year:string, UserId:integer"
    
  2. As in the previous step, two files were created, one for migration of data and another to be used as a Band model, as the following code:
          'use strict'; 
          module.exports = function(sequelize, DataTypes) { 
            var Band = sequelize.define('Band', { 
              name: DataTypes.STRING, 
              description: DataTypes.STRING, 
              album: DataTypes.STRING, 
              year: DataTypes.STRING, 
              UserId: DataTypes.INTEGER 
            }, { 
              classMethods: { 
                 associate: function(models) { 
                  // associations can be defined here 
              } 
            } 
          }); 
          return Band; 
          }; 
    

Creating associations between Band and User models

As the last step before using the schemes migration script, we will need to create the associations between the User model and the Band model. We will use the following associations:

Tip

You can find more about associations at the following link: http://docs.sequelizejs.com/en/latest/docs/associations/.

  1. Open the User.js model and add the following highlighted code:
          'use strict'; 
           module.exports = function(sequelize, DataTypes) { 
             var User = sequelize.define('User', { 
               name: DataTypes.STRING, 
               email: DataTypes.STRING 
             }, { 
               classMethods: { 
                 associate: function(models) { 
                  // associations can be defined here 
                  User.hasMany(models.Band); 
                } 
              } 
             }); 
           return User; 
          }; 
    
  2. Open the Band.js model and add the following highlighted code:
          'use strict'; 
           module.exports = function(sequelize, DataTypes) { 
             var Band = sequelize.define('Band', { 
               name: DataTypes.STRING, 
               description: DataTypes.STRING, 
               album: DataTypes.STRING, 
               year: DataTypes.STRING, 
               UserId: DataTypes.INTEGER 
             }, { 
               classMethods: { 
                  associate: function(models) { 
                  // associations can be defined here 
            Band.belongsTo(models.User); 
             } 
            } 
           }); 
           return Band; 
          }; 
    
主站蜘蛛池模板: 桦川县| 安国市| 浦城县| 韶关市| 固始县| 九龙城区| 温泉县| 西华县| 宁陕县| 应城市| 皮山县| 清水县| 廉江市| 深州市| 宜城市| 汉中市| 大石桥市| 英山县| 曲松县| 衢州市| 安西县| 湘西| 论坛| 温州市| 昌宁县| 合山市| 宁陕县| 任丘市| 安阳市| 沙坪坝区| 花莲市| 葵青区| 汾西县| 武清区| 镇原县| 文成县| 于田县| 宜良县| 安乡县| 呼玛县| 喀喇沁旗|