我有一个我需要使用的现有数据库 .
用户表看起来像这样:
id
name
storeId
商店表看起来有点像这样:
id
name
address
用户可以只与一个商店相关联 . 我希望能做的是:
User.hasOne(models.Store, { foreignKey: 'storeId' } ); // Working with existing db, so I need to specify the column name
然后,能够做这样的事情:
User.find(1, { include: [models.Store] })
但是,这样做并不起作用 . 我收到以下错误:
Unknown column 'Store.storeId' in 'field list'
当我收到错误时,它正在尝试运行此查询:
SELECT `user`.*, `store`.`id` AS `store.id`, `store`.`name` AS `store.name`, `store`.`storeId` AS `store.storeId` FROM `user` LEFT OUTER JOIN `store` AS `store` ON `user`.`id` = `store`.`storeId` WHERE `user`.`id`=1 LIMIT 1;
这显然是错的 . 它应该加入 user.storeId = store.id
,它不应该试图选择 store.storeId
.
我无法弄清楚如何使这项工作 - 任何提示?
我正在使用sequelize v1.7.9
1 回答
试试这个