我有两个model Blog,Catalog Blog的代码大致是这样 sequelize.define(“Blog”,{ blogId:{xxx}, catalogId:{xxx}, … } Catalog的代码大致是这样 sequelize.define(“Catalog”,{ catalogId:{xxx}, … } 现在想做关联查询于是我写了他们之间的关系: Blog.belongsTo(Catalog,{foreignKey:‘catalogId’}); Catalog.hasMany(Blog,{as:‘Catalog’});
然而生成的代码是这样的:
SELECT Blog
.blogId
, Blog
.catalogId
, …,Blog
.CatalogCatalogId
, Catalog
.catalogId
AS Catalog.catalogId
,… FROM blog
AS Blog
LEFT OUTER JOIN catalog
AS Catalog
ON Blog
.catalogId
=
Catalog
.catalogId
;
我就想问下为什么会生成一个加粗部分,我是哪里写得有问题?说实话sequelize的文档真的好难看
就是那个CatalogCatalogId字段,我根本没定义过这个字段啊
对了,我把Catalog.hasMany(Blog,{as:‘Catalog’});这句话去掉,就能正确查出结果了,是我画蛇添足了吗,第一次用orm