续集 - 向表添加新行并关联表

Sequelize - Add new row to table and to associate table

本文关键字:关联 新行 添加 续集      更新时间:2023-09-26

>我正在使用 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);
                    });
            })
     });
 }

希望有人觉得它有用。