egg-sequelize创建表

在app/model/下创建表模型:

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const User = app.model.define('user', {
    username: STRING(30),
    password: {
      type: STRING(32),
      comment: "md5存储"
    },
    name: {
      type: STRING(15),
      comment: "给谁用的,显示的名"
    },
    // 0无效,1有效,...
    status: {
      type: INTEGER,
      comment: "0无效,1有效,无效状态下无法登陆"
    },
    // 角色
    role_id: INTEGER,
  });

  User.associate = function () {
    
    app.model.User.belongsTo(app.model.Role, {
      foreignKey: "role_id",
    })

    // 查看记录
    app.model.User.hasMany(app.model.TalentViewRecord, {
      foreignKey: "user_id",
    })

  }

  // 修改表结构或创建表
  // User.sync({alter:true})

  return User;
};
User.sync({alter:true})  // 单表的创建或修改表结构

整体创建:

# app/router.js

app.beforeStart(async () => {
    // await app.model.sync({ alter: true });//force  false 为不覆盖 true会删除再创建; alter true可以 添加或删除字段;
  });
原文地址:https://www.cnblogs.com/zezhou/p/15129450.html