added foreignKey: 'parentId' to hasMany

This commit is contained in:
notnull 2019-07-09 12:35:14 -04:00
parent 31087e4be5
commit f458bcd5ab
2 changed files with 12 additions and 13 deletions

View File

@ -1,6 +1,9 @@
const Comment = require('./comment')
Comment.belongsTo(Comment, { as: 'parent' })
Comment.hasMany(Comment, { as: { singular: 'reply', plural: 'replies' } })
Comment.hasMany(Comment, {
as: { singular: 'reply', plural: 'replies' },
foreignKey: 'parentId',
})
module.exports = { Comment }

View File

@ -1,13 +1,12 @@
const db = require('../db')
const { Comment } = require('./models')
const tc1 = {text: 'c1'}
const tc2 = {text: 'c2'}
const tc3 = {text: 'c3'}
const tc4 = {text: 'c4'}
const tc5 = {text: 'c5'}
const tc6 = {text: 'c6'}
const tc1 = { text: 'c1' }
const tc2 = { text: 'c2' }
const tc3 = { text: 'c3' }
const tc4 = { text: 'c4' }
const tc5 = { text: 'c5' }
const tc6 = { text: 'c6' }
async function runSeed() {
await db.sync({ force: true })
@ -22,13 +21,10 @@ async function runSeed() {
const c6 = await Comment.create(tc6)
await c1.addReply(2)
//await c2.setParent(1)
await c2.addReply(3)
//await c3.setParent(2)
//await c4.setParent(2)
await c2.addReplies([c3, c4, c5])
await c6.setParent(c1)
console.log('seeded successfully')
} catch (err) {