article.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. const {
  3. Model
  4. } = require('sequelize');
  5. module.exports = (sequelize, DataTypes) => {
  6. class Article extends Model {
  7. /**
  8. * Helper method for defining associations.
  9. * This method is not a part of Sequelize lifecycle.
  10. * The `models/index` file will call this method automatically.
  11. */
  12. static associate(models) {
  13. // 文章与分类的关联关系
  14. Article.belongsTo(models.Category, {
  15. foreignKey: 'category',
  16. as: 'categoryInfo'
  17. });
  18. }
  19. }
  20. Article.init({
  21. title: {
  22. type:DataTypes.TEXT,
  23. allowNull:false,
  24. validate:{
  25. notNull:{
  26. msg:'标题必须存在'
  27. },
  28. notEmpty:{
  29. msg:'标题不能为空'
  30. },
  31. len:{
  32. args:[1,500],
  33. msg:'标题长度需要在1~500个字符之间'
  34. }
  35. }
  36. },
  37. content: DataTypes.TEXT('long'),
  38. type:DataTypes.INTEGER,
  39. img:DataTypes.TEXT,
  40. date:DataTypes.DATE,
  41. author:DataTypes.TEXT,
  42. category:DataTypes.INTEGER,
  43. crop:DataTypes.INTEGER,
  44. seoKeyword:DataTypes.TEXT,
  45. seoDescription:DataTypes.TEXT,
  46. }, {
  47. sequelize,
  48. modelName: 'Article',
  49. });
  50. return Article;
  51. };