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

数据表设计和迁移


创建数据迁移表

shell
npx sequelize migration:generate --name=comment

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

js
"use strict";

module.exports = {
  up: (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM, TEXT } = Sequelize;
    return queryInterface.createTable("comment", {
      id: {
        type: INTEGER(20),
        primaryKey: true,
        autoIncrement: true,
      },
      content: {
        type: TEXT,
        allowNull: false,
        defaultValue: "",
        comment: "评论内容",
      },
      video_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "视频id",
        references: {
          model: "video",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      user_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "用户id",
        references: {
          model: "user",
          key: "id",
        },
        onDelete: "cascade",
        onUpdate: "restrict", // 更新时操作
      },
      reply_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "回复id",
      },
      reply_user_id: {
        type: INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "回复用户id",
      },
      created_time: DATE,
      updated_time: DATE,
    });
  },

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

模型创建

js
// app/model/comment.js
module.exports = (app) => {
  const { STRING, INTEGER, DATE, ENUM, TEXT } = app.Sequelize;

  const Comment = app.model.define("comment", {
    id: {
      type: INTEGER(20),
      primaryKey: true,
      autoIncrement: true,
    },
    content: {
      type: TEXT,
      allowNull: false,
      defaultValue: "",
      comment: "评论内容",
    },
    video_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "视频id",
      references: {
        model: "video",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "用户id",
      references: {
        model: "user",
        key: "id",
      },
      onDelete: "cascade",
      onUpdate: "restrict", // 更新时操作
    },
    reply_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "回复id",
    },
    reply_user_id: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "回复用户id",
    },
    created_time: DATE,
    updated_time: DATE,
  });

  // 关联关系
  Comment.associate = function (models) {
    // 关联作者
    Comment.belongsTo(app.model.User, {
      foreignKey: "user_id",
      as: "send_user",
    });
    // 关联被回复人
    Comment.belongsTo(app.model.User, {
      foreignKey: "reply_user_id",
      as: "reply_user",
    });
    // 关联视频
    Comment.belongsTo(app.model.Video);

    // 关联回复
    Comment.hasMany(app.model.Comment, {
      foreignKey: "reply_id",
    });
  };

  return Comment;
};