Node.js “继续嵌套关系”;职位;有多个;作者;?
我在跟博士后学习续集。如何使“帖子”具有多个作者?我目前每篇文章只有一位作者。我尝试使用Node.js “继续嵌套关系”;职位;有多个;作者;?,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我在跟博士后学习续集。如何使“帖子”具有多个作者?我目前每篇文章只有一位作者。我尝试使用Post.hasMany(Person),但我不知道如何在下面的sync语句中填充数据,因为我是从教程中复制的。。。这是我第一次使用数据库 import Sequelize from 'sequelize'; import _ from 'lodash'; import Faker from 'faker'; const Conn = new Sequelize( 'test_db', 'p
Post.hasMany(Person)
,但我不知道如何在下面的sync语句中填充数据,因为我是从教程中复制的。。。这是我第一次使用数据库
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;
尝试:
如果要命名中间表,还可以指定有关M2M的更多详细信息,例如:
Post.belongsToMany(Person, {
as: 'Authors',
through: 'post_authors',
foreignKey: 'id',
otherKey: 'id'
})
我想知道如何以一种很好的方式将这些条目添加到这个post_authors表中,我尝试了这个方法,但效果不太好:我也在看这个帖子:
Post.belongsToMany(Person, {
as: 'Authors',
through: 'post_authors',
foreignKey: 'id',
otherKey: 'id'
})