Last active
July 29, 2020 02:00
-
-
Save derduskenga/6745e8347276d916d4068637c2184a73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const { | |
Model | |
} = require('sequelize'); | |
module.exports = (sequelize, DataTypes) => { | |
class Post extends Model { | |
/** | |
* Helper method for defining associations. | |
* This method is not a part of Sequelize lifecycle. | |
* The `models/index` file will call this method automatically. | |
*/ | |
static associate(models) { | |
// define association here | |
Post.belongsTo(models.User); | |
//post many to many association with Tag | |
Post.belongsToMany(models.Tag,{ | |
through: models.PostTag, | |
foreignKey:'tag_id' | |
}); | |
} | |
}; | |
Post.init({ | |
post_title: DataTypes.STRING, | |
post_text: DataTypes.TEXT, | |
status: DataTypes.BOOLEAN, | |
userId: { | |
type: DataTypes.INTEGER, | |
references:{ | |
model:'User', | |
key:'id' | |
} | |
} | |
}, { | |
sequelize, | |
modelName: 'Post', | |
}); | |
return Post; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment