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

设置备注和标签


具体看课程演示

标签表

shell
npx sequelize migration:generate --name=tag
js
"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, STRING, DATE, ENUM } = Sequelize;
    // 创建表
    await queryInterface.createTable("tag", {
      id: {
        type: INTEGER(20).UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: STRING(30),
        allowNull: false,
        defaultValue: "",
        comment: "标签名称",
      },
      user_id: {
        type: INTEGER(20).UNSIGNED,
        allowNull: false,
        comment: "用户id",
        //  定义外键(重要)
        references: {
          model: "user", // 对应表名称(数据表名称)
          key: "id", // 对应表的主键
        },
        onUpdate: "restrict", // 更新时操作
        onDelete: "cascade", // 删除时操作
      },
      created_at: DATE,
      updated_at: DATE,
    });
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable("tag");
  },
};

标签好友关联表

shell
npx sequelize migration:generate --name=friend_tag
js
"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, DATE } = Sequelize;
    // 创建表
    await queryInterface.createTable("friend_tag", {
      id: {
        type: INTEGER(20).UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      friend_id: {
        type: INTEGER(20).UNSIGNED,
        allowNull: false,
        comment: "好友id",
        //  定义外键(重要)
        references: {
          model: "friend", // 对应表名称(数据表名称)
          key: "id", // 对应表的主键
        },
        onUpdate: "restrict", // 更新时操作
        onDelete: "cascade", // 删除时操作
      },
      tag_id: {
        type: INTEGER(20).UNSIGNED,
        allowNull: false,
        comment: "标签id",
        //  定义外键(重要)
        references: {
          model: "tag", // 对应表名称(数据表名称)
          key: "id", // 对应表的主键
        },
        onUpdate: "restrict", // 更新时操作
        onDelete: "cascade", // 删除时操作
      },
      created_at: DATE,
      updated_at: DATE,
    });
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable("friend_tag");
  },
};