I am trying to create a channelentry that belongs to channels and users. I am getting a foreign key for the user column but im having trouble getting a foreign key for my channel.
router.post("/create/:id", validateJWT, async (req, res) => {
const {entry} = req.body.channelentry
const channelMessage = {
entry,
userId: req.user.id
}
try {
const currentChannelId = await models.ChannelModel.findOne({
where: {
id: req.params.id
}
})
const newChannel = await models.ChannelEntryModel.create(channelMessage, currentChannelId);
res.status(200).json(newChannel)
} catch (err) {
res.status(500).json({error: err})
}
i did find one way to fill the channel foreign key but that also has issues.
I did a findOrCreate method that does return a foreign key but if there is already an entry that matches it will not create a new entry.
router.post("/create/:id", validateJWT, async (req, res) => {
const {entry} = req.body.channelentry
try {
const newChannel = await models.ChannelEntryModel.findOrCreate({
where: {
entry,
userId: req.user.id,
channelId: req.params.id
}
})
res.status(200).json(newChannel)
} catch (err) {
res.status(500).json({error: err})
}
is there something i am missing in the first approach or is there a way to create a new entry even if an entry matches for a findOrCreate method.