Add article and association with user
This commit is contained in:
parent
f28f399f89
commit
b4f2ccd8fe
3 changed files with 95 additions and 7 deletions
33
models/article.js
Normal file
33
models/article.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
'use strict';
|
||||
const {
|
||||
Model
|
||||
} = require('sequelize');
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class article extends Model {
|
||||
static associate(models) {
|
||||
Article.belongsTo(models.User, {
|
||||
foreignKey: 'submitted_by',
|
||||
onDelete: 'SET NULL',
|
||||
onUpdate: 'CASCADE',
|
||||
as: 'submitter',
|
||||
})
|
||||
Article.belongsTo(models.User, {
|
||||
as: 'reviewer',
|
||||
foreignKey: 'reviewed_by',
|
||||
onDelete: 'SET NULL',
|
||||
onUpdate: 'CASCADE',
|
||||
})
|
||||
}
|
||||
}
|
||||
article.init({
|
||||
title: DataTypes.STRING,
|
||||
content: DataTypes.STRING,
|
||||
submittedBy: DataTypes.STRING,
|
||||
reviewedBy: DataTypes.STRING
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'article',
|
||||
});
|
||||
return article;
|
||||
};
|
||||
|
|
@ -4,13 +4,18 @@ const {
|
|||
} = require('sequelize');
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class user 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
|
||||
User.hasMany(models.Article, {
|
||||
foreignKey: 'submitted_by',
|
||||
onDelete: 'SET NULL',
|
||||
onUpdate: 'CASCADE',
|
||||
})
|
||||
User.hasMany(models.Article, {
|
||||
foreignKey: 'reviewed_by',
|
||||
as: 'reviewedArticles',
|
||||
onDelete: 'SET NULL',
|
||||
onUpdate: 'CASCADE',
|
||||
})
|
||||
}
|
||||
}
|
||||
user.init({
|
||||
|
|
@ -20,4 +25,4 @@ module.exports = (sequelize, DataTypes) => {
|
|||
modelName: 'user',
|
||||
});
|
||||
return user;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue