polymorphic.js 526 B

12345678910111213141516171819202122232425
  1. var Comment = sequelize.define('comment')
  2. var Commentable = Sequelize.polymorphic('commentable', {
  3. })
  4. Comment.belongsTo(Commentable)
  5. // or
  6. var Commentable = Comment.polymorphic('commentable', {
  7. 'type_column': 'commentable_type',
  8. 'key_column': 'commentable_id',
  9. as: 'Parent'
  10. })
  11. Question.hasMany(Commentable) //type = question by defualt
  12. Question.hasMany(Commentable, {
  13. as: 'Answer'
  14. type: 'answer'
  15. })
  16. Question.findAll({
  17. include: [
  18. Commentable,
  19. {model: Commentable, as: 'Answer'}
  20. ]
  21. })
  22. ForumPost.hasMany(Commentable)