I'm learning Sequelize with Postgres. How can I make the "post" to have multiple authors? I currently have only one author per post. I tried using Post.hasMany(Person)
but I couldn't figure out how to fill in the data in the sync statement below, because I copied it from a tutorial... It's my first time using a database.
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;