poll_question.js 687 B

1234567891011121314151617181920212223242526272829303132
  1. module.exports = (sequelize, DataTypes) => {
  2. let Sequelize = sequelize.Sequelize
  3. let PollQuestion = sequelize.define('PollQuestion', {
  4. question: {
  5. type: DataTypes.STRING,
  6. allowNull: false,
  7. validate: {
  8. len: {
  9. args: [1, 256],
  10. msg: 'The question must be between 1 and 256 characters'
  11. },
  12. isString (val) {
  13. if(typeof val !== 'string') {
  14. throw new Sequelize.ValidationError('The question must be a string')
  15. }
  16. }
  17. }
  18. }
  19. }, {
  20. classMethods: {
  21. associate (models) {
  22. PollQuestion.belongsTo(models.User)
  23. PollQuestion.hasMany(models.PollAnswer, {
  24. foreignKey: { name: 'question_id' }
  25. })
  26. }
  27. }
  28. })
  29. return PollQuestion
  30. }