'Handling foreign key on a post route Prisma / MYSQL
i would like to understand why my post route doesn't work, the error comes out like :
ReferenceError: author_id is not defined
Here is my code following :
Schema Prisma :
model users {
user_id Int @id @default(autoincrement()) @db.UnsignedInt
firstName String @db.VarChar(255)
lastName String @db.VarChar(255)
email String @unique(map: "email") @db.VarChar(255)
password String @db.VarChar(255)
createdAt DateTime @default(now())
isAdmin Boolean @default(false)
post post[]
comments comments[]
}
model post {
post_id Int @id @default(autoincrement()) @db.UnsignedInt
title String @db.VarChar(50)
content String @db.Text
author_id Int @db.UnsignedInt
users users @relation(fields: [author_id], references: [user_id])
comments comments[]
@@index([author_id], name: "FK1_AUTHORID")
}
And my post.route.js :
router.post('/', async (req, res) => {
try {
const { title, content } = req.body;
const result = await prisma.post.create({
data: {
title,
content,
users: {
connect: {
user_id: author_id
}
},
},
})
res.json(result)
}
catch(e) {
console.log(e);
}
});
Everything on the database is up to date, the tables are exactly like the schema i sent
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
