将嵌套关系序列化,有多个“作者”的“post”?

我正在用Postgres学习Sequelize。 我怎样才能使“post”有多个作者? 我目前每个post只有一个作者。 我尝试使用Post.hasMany(Person)但我不知道如何填写下面的同步声明中的数据,因为我从教程中复制它…这是我第一次使用数据库。

 import Sequelize from 'sequelize'; import _ from 'lodash'; import Faker from 'faker'; const Conn = new Sequelize( 'test_db', 'postgres', 'postgres', { dialect: 'postgres', host: 'localhost' } ); const Person = Conn.define('person', { firstName: { type: Sequelize.STRING, allowNull: false }, lastName: { type: Sequelize.STRING, allowNull: false } }); const Post = Conn.define('post', { title: { type: Sequelize.STRING, allowNull: false }, content: { type: Sequelize.STRING, allowNull: false } }); // Relationships Person.hasMany(Post); Post.belongsTo(Person); Conn.sync({ force: true }).then(() => { _.times(10, () => { return Person.create({ firstName: Faker.name.firstName(), lastName: Faker.name.lastName() }).then(person => { return person.createPost({ title: `Sample title by ${person.firstName}`, content: 'This is a sample article.' }) }) }); }); export default Conn; 

尝试:

 Post.belongsToMany(Person); 

您也可以指定关于M2M的更多详细信息,例如,如果您希望命名中间表,例如:

  Post.belongsToMany(Person, { as: 'Authors', through: 'post_authors', foreignKey: 'id', otherKey: 'id' })