续集 - 向表添加新行并关联表
Sequelize - Add new row to table and to associate table
>我正在使用 MySql 的 Sequelize ORM,目前在表和关联表中创建新条目时遇到了问题。 我的模型: 文章
module.exports = function (sequelize, DataTypes) {
var Articles = sequelize.define('articles', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
author: {
type: DataTypes.STRING
},
title: {
type: DataTypes.STRING
},
intro: {
type: DataTypes.TEXT
},
article: {
type: DataTypes.TEXT
},
cd: {
type: DataTypes.DATE
}
}, {
createdAt: 'cd',
updatedAt: false,
freezeTableName: true,
classMethods: {
associate: function (models) {
this.belongsToMany(models.comments, {
through: 'commented_articles'
});
}
}
});
return Articles;
};
Comments:
module.exports = function (sequelize, DataTypes) {
var Comments = sequelize.define('comments', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
id_parent: {
type: DataTypes.INTEGER,
defaultValue: null
},
user_id: {
type: DataTypes.INTEGER,
defaultValue: null
},
nick: {
type: DataTypes.STRING
},
title: {
type: DataTypes.STRING
},
comment: {
type: DataTypes.TEXT
},
plus: {
type: DataTypes.INTEGER,
defaultValue: null,
validate: {
isInt: {
msg: "Must be an integer number."
}
}
},
minus: {
type: DataTypes.INTEGER,
defaultValue: null,
validate: {
isInt: {
msg: "Must be an integer number."
}
}
}
}, {
createdAt: 'cd',
updatedAt: false,
freezeTableName: true,
classMethods: {
associate: function (models) {
this.belongsToMany(models.articles, {
through: 'commented_articles'
});
}
}
});
return Comments;
};
I want to add new comment to article, so probably my routing would look like
module.exports = function (app) {
var articles = require('./../apiMethods/articlesMethods');
app.post('/articles/:id/comments', articles.getSingleArticle);
};
I was trying to use addComment (http://docs.sequelizejs.com/en/latest/docs/associations/)
var models = require('../models'),
MyModel = require('../supportMethods/modelMethods'),
supportModel = new MyModel(models.articles);
module.exports = (function () {
return {
setArticle: function (req, res) {
models.articles.addComments(models.comments, {
user_id: req.body.user_id,
nick: req.body.nick,
title: req.body.title,
comment: req.body.comment
})
}
};
})();
The "commented_articles" is obviously automaticaly generated by squelize and contains article_id, comment_id and cd (row update timestamp).
我想要实现的是向适当的文章添加评论,同时在"commented_articles"中添加新单元格。"评论"表包含对文章和图片的评论(还有一个"commented_images"表)。我试图使用addComments方法,但它不起作用。我做错了什么? 谢谢
我已经解决了我的问题。脏版本是:
addNewCommentToArticle: function (req, res) {
var id_parent = req.body.id_parent,
user_id = req.body.user_id,
nick = req.body.nick,
title = req.body.title,
comment = req.body.comment;
models.articles.findByPrimary(req.params.id)
.then(function (createdArticle) {
models.comments.create({
id_parent: id_parent,
user_id: user_id,
nick: nick.trim(),
title: title.trim(),
comment: comment.trim()
}).then(function (newComment) {
createdArticle.addComments(newComment)
.then(function () {
res.json(newComment);
})
.catch(function (e) {
res.json(e);
});
})
});
}
希望有人觉得它有用。
相关文章:
- 如何从rails中的代码中删除新行( )
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- 如何在脚本上添加新行
- 在不刷新整个网格的情况下,向Asp.net网格视图添加新行
- 字符串中的
标记未正确在 HTML 中创建新行 - JavaScript-如果以前不存在文本,如何从文本区域删除新行
- 反应中的新行
- 阻止ReSharper将JavaScript函数参数放到新行中
- HTML表内容:如果行很长,如何在新行中显示部分内容
- dhtmlx添加新行并启动单元格编辑器
- 将新行打印到$mdToast
- 在新行(html)中动态添加更多文本字段
- 使用删除文本创建新行
- 如何在Google Geochart图表的工具提示文本中添加新行
- NodeJS Fork-每次子流程发送新行时都进行React
- 为什么要将此p标记添加为新行
- 如何转义php中的新行字符以输出为javascript字符串值
- 在Aptana Studio 3中将Javascript数组自动格式化为新行
- Jquery在表WITH函数中追加新行后添加单击事件
- 续集 - 向表添加新行并关联表