【问题标题】:Issue with relations in SequelizeJSSequelizeJS 中的关系问题
【发布时间】:2014-09-11 17:51:44
【问题描述】:

我在 SequelizeJS 中使用关系时遇到问题。

我的错误信息:(500 : Internal Server Error) 用户与文章没有关联!

文章型号:

var Article = db.define('article', {

    title: {
        type: Types.STRING,
    },

    content: {
        type: Types.TEXT,
    },
},{
    classMethods:{
        associate: function (models) {
            Article.belongsTo(models.User, {as: 'Author'});
        }
    }
});

用户模型:

var User = db.define('user', {

    username: {
        type: Types.STRING,
    },

    email: {
        type: Types.STRING,
    },

    password: {
        type: Types.STRING
    },
},{
    classMethods:{
        associate: function (models) {
            User.hasMany(models.Article, {as: 'Articles'});
        }
    }
});

还有我的包含查询:

getAll: function (callback) {
    return this.context.Article.findAll({include: [this.context.User]});
},

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:
    Article.findAll({include: [{ model: this.context.User, as: 'Author'}]});
    

    因为关系有别名(作者),所以您也必须在预先加载时提供该别名

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 2019-04-20
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多