sequelize Model.hasOne Error: Model is not associated to ModelTwo

纵饮孤独 提交于 2019-12-04 22:34:32

This is actually another case which throws the same error,

I ran in to the same issue when I try to use as option for aliasing when including the model. This will not throw an error as long as you aliased the default name, but you'll get error if you try different name.

The solution,

It clearly says

If an association is aliased (using the as option), you must specify this alias when including the model.

This means that, the vise versa as well

If you want to alias a model when including, it's association must be aliased (using the as option).

I figured it out. It seems I needed to use the following in the model definition to add associations instead of the simple Model.hasOne(bla) which is in the documentation.

classMethods:{
  associate:function(models){
    Model.hasOne(models.ModelTwo,{foreignKey:'foreign_key'})
  }
}

This code was in the tutorial for express on their site, not in the association documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!