koa2 中建表方法

发布时间 2023-04-19 11:22:01作者: 倍姬存希

需求,在model层新建商品表

一.在model目录下新建goods.model.js

const { DataTypes } = require('sequelize')

const seq = require('../db/seq')

const Goods = seq.define(
  'zd_goods',
  {
    goods_name: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '商品名称',
    },
    goods_price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '商品价格',
    },
    goods_num: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '商品库存',
    },
    goods_img: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '商品图片的url',
    },
  },
  {
    paranoid: true,
  }
)
Goods.sync({ force: true }) module.exports = Goods

例如

 二:终端执行,node src/model/goods.model.js

建完之后,数据库中就会多一张zd_goods表

注意:建完表之后将Goods.sync({ force: true })注释,否则下次再继续执行这句时,会将表中数据全部清掉,初始化

// Goods.sync({ force: true })

module.exports = Goods