added foreignKey: 'parentId' to hasMany
This commit is contained in:
parent
31087e4be5
commit
f458bcd5ab
@ -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 }
|
||||
|
20
db/seed.js
20
db/seed.js
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user