使用Sequelize时,我是否必须遵循他们的表/字段名称模式



我正在使用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`

但是我已经有了表,我不想将键的名称更改为fooNamebarTitle。这是只使用nametitle的方法吗?

谢谢。

根据sequelize docs 中的BelongsToMany规范更正您的定义

User.belongsToMany(Project, { as: 'Tasks', through: 'worker_tasks', foreignKey: 'userId', otherKey: 'projectId'})

sourceKey替换为foreignKey,将targetKeytherKey

相关内容

最新更新