Skip to content
关注公众号,获取新课通知

数据表设计和迁移


创建数据迁移表

shell
npx sequelize migration:generate --name=user

1.执行完命令后,会在database / migrations / 目录下生成数据表迁移文件,然后定义

js
"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM } = Sequelize;
    // 创建表
    await queryInterface.createTable("user", {
      id: {
        type: INTEGER(20).UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      username: {
        type: STRING(30),
        allowNull: false,
        defaultValue: "",
        comment: "用户名称",
        unique: true,
      },
      nickname: {
        type: STRING(30),
        allowNull: false,
        defaultValue: "",
        comment: "...",
      },
      email: {
        type: STRING(160),
        comment: "用户邮箱",
        unique: true,
      },
      password: {
        type: STRING(200),
        allowNull: false,
        defaultValue: "",
      },
      avatar: {
        type: STRING(200),
        allowNull: true,
        defaultValue: "",
      },
      phone: {
        type: STRING(20),
        comment: "用户手机",
        unique: true,
      },
      sex: {
        type: ENUM,
        values: ["男", "女", "保密"],
        allowNull: true,
        defaultValue: "男",
        comment: "用户性别",
      },
      status: {
        type: INTEGER(1),
        allowNull: false,
        defaultValue: 1,
        comment: "状态",
      },
      sign: {
        type: STRING(200),
        allowNull: true,
        defaultValue: "",
        comment: "个性签名",
      },
      area: {
        type: STRING(200),
        allowNull: true,
        defaultValue: "",
        comment: "地区",
      },
      created_at: DATE,
      updated_at: DATE,
    });
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable("user");
  },
};
  • 执行 migrate 进行数据库变更
shell
npx sequelize db:migrate

模型创建

js
// app/model/user.js
"use strict";
module.exports = (app) => {
  const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;
  // 配置(重要:一定要配置详细,一定要!!!)
  const User = app.model.define("user", {
    id: {
      type: INTEGER(20).UNSIGNED,
      primaryKey: true,
      autoIncrement: true,
    },
    username: {
      type: STRING(30),
      allowNull: false,
      defaultValue: "",
      comment: "用户名称",
      unique: true,
    },
    nickname: {
      type: STRING(30),
      allowNull: false,
      defaultValue: "",
      comment: "...",
    },
    email: {
      type: STRING(160),
      comment: "用户邮箱",
      unique: true,
    },
    password: {
      type: STRING(200),
      allowNull: false,
      defaultValue: "",
    },
    avatar: {
      type: STRING(200),
      allowNull: true,
      defaultValue: "",
    },
    phone: {
      type: STRING(20),
      comment: "用户手机",
      unique: true,
    },
    sex: {
      type: ENUM,
      values: ["男", "女", "保密"],
      allowNull: true,
      defaultValue: "男",
      comment: "用户性别",
    },
    status: {
      type: INTEGER(1),
      allowNull: false,
      defaultValue: 1,
      comment: "状态",
    },
    sign: {
      type: STRING(200),
      allowNull: true,
      defaultValue: "",
      comment: "个性签名",
    },
    area: {
      type: STRING(200),
      allowNull: true,
      defaultValue: "",
      comment: "地区",
    },
    created_at: DATE,
    updated_at: DATE,
  });
  return User;
};