我正在使用 Node 和 Sequelize 来访问现有数据库。现在我正在尝试在 Sequelize 中创建一个 n:m 关联,但是我在字段名称方面遇到了一些问题。
根据 Sequelize 文档:
Foo.belongsToMany(Bar, { through: 'foo_bar', sourceKey: 'name', targetKey: 'title' });
// This creates a junction table `foo_bar` with fields `fooName` and `barTitle`
但是我已经有了表,我不想将键的名称更改为
fooName
和 barTitle
。这是一种只使用 name
和 title
的方法吗?谢谢你。
最佳答案
根据 sequelize docs 中的 BelongsToMany 规范更正您的定义
User.belongsToMany(Project, { as: 'Tasks', through: 'worker_tasks', foreignKey: 'userId', otherKey: 'projectId'})
将 sourceKey 替换为 foreignKey 并将 targetKey 替换为 otherKey
关于javascript - 使用 Sequelize 时,我是否必须遵循他们的表/字段名称模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61233525/