这有点奇怪。 Sequelize 查询在...运行 1 年后停止工作。
const where = { ... };
const attributes = ['id', 'code']; // This one making problems
const include = [...];
return SiteDao.findAll({ where, include, attributes });
我真的不明白这一点,因为一切都运行良好,甚至尝试记录查询并直接在数据库上尝试它们,并且它们起作用了。然后我发现属性破坏了它......但为什么呢?这对我有用const where = { ... };
const attributes = { include: ['id', 'code'] }; // This one works for me
const include = [...];
return SiteDao.findAll({ where, include, attributes });
我不太明白,因为 official documentation 使用了第一个版本的属性我的问题是,为什么会发生这种情况,是否有可能以某种方式修复它,就像其他所有 api 都在属性上使用投影一样......
最佳答案
我认为这个文档有点过时了,或者我怀疑这适用于比您正在使用的更早版本的 sequelize。官方文档解释了如何使用这两个选项。 https://sequelize.org/master/class/lib/model.js~Model.html#static-method-findAll
两个都
const attributes = ['id', 'code'];
const attributes = { include: ['id', 'code'] };
应该是有效的选项。我怀疑 ['id', 'code'] 在 sql 中像列 'id' 被翻译为 'code' 因为有一个选项可以更改属性的名称。你应该试试 const attributes = [['id','id'], ['code','code']];
如果您想保持代码一致以保持清晰。
关于node.js - 用属性 : []? 续写最近的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66130856/