20250912075346-create-category.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // migrations/20250225000000-create-categories-table.js
  2. 'use strict';
  3. module.exports = {
  4. async up(queryInterface, Sequelize) {
  5. await queryInterface.createTable('categories', {
  6. id: {
  7. type: Sequelize.INTEGER,
  8. primaryKey: true,
  9. autoIncrement: true
  10. },
  11. name: {
  12. type: Sequelize.STRING,
  13. allowNull: false
  14. },
  15. level: {
  16. type: Sequelize.INTEGER,
  17. defaultValue: 1
  18. },
  19. description: {
  20. type: Sequelize.TEXT,
  21. allowNull: true
  22. },
  23. parentId: {
  24. type: Sequelize.INTEGER,
  25. allowNull: true,
  26. references: {
  27. model: 'categories',
  28. key: 'id'
  29. },
  30. onDelete: 'CASCADE'
  31. },
  32. order: {
  33. type: Sequelize.INTEGER,
  34. defaultValue: 0
  35. },
  36. isActive: {
  37. type: Sequelize.BOOLEAN,
  38. defaultValue: true
  39. },
  40. createdAt: {
  41. type: Sequelize.DATE,
  42. allowNull: false
  43. },
  44. updatedAt: {
  45. type: Sequelize.DATE,
  46. allowNull: false
  47. }
  48. });
  49. // 添加索引
  50. await queryInterface.addIndex('categories', ['parentId']);
  51. await queryInterface.addIndex('categories', ['name']);
  52. },
  53. async down(queryInterface, Sequelize) {
  54. await queryInterface.dropTable('categories');
  55. }
  56. };