我正在使用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,将targetKeytherKey