且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

sequelize Model.hasOne 错误:模型未关联到 ModelTwo

更新时间:2023-11-17 16:31:28

这其实是另一种情况,抛出同样的错误,

This is actually another case which throws the same error,

当我在包含模型时尝试使用 as 选项进行别名时遇到了同样的问题.只要您为默认名称起别名,这不会引发错误,但如果您尝试不同的名称,则会出错.

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.

解决办法,

上面写的很清楚

如果关联是别名(使用 as 选项),您必须指定包含模型时的此别名.

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

这意味着,反之亦然

如果你想在包含模型时给模型起别名,它的关联必须是别名(使用 as 选项).

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