【问题标题】:Sequelize - Add new row to table and to associate tableSequelize - 向表中添加新行并关联表
【发布时间】:2016-02-29 20:50:41
【问题描述】:

我在 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).

我想要实现的是将 cmets 添加到适当的文章中,同时在“commented_articles”中添加新单元格。 “cmets”表包含文章和图片的 cmets(还有一个“commented_images”表)。 我试图使用 addComments 方法,但它不起作用。我究竟做错了什么? 谢谢

【问题讨论】:

  • 我的问题已经解决了:

标签: javascript mysql node.js express sequelize.js


【解决方案1】:

我已经解决了我的问题。脏版是:

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);
                    });
            })
     });
 }

希望有人觉得它有用。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多