插入数据的代码
router.post("/add", async ctx => {
const { title, content, userId } = ctx.request.body;
try {
let data = await Article.create({
title,
content,
userId
});
console.log(data);
if (data) {
ctx.body = {
code: 200,
msg: "ok",
data
};
}
} catch (error) {
ctx.body = {
code: -1,
msg: "创建失败",
error
};
}
});
外键关联的代码
// 外键关联 多对一
Article.belongsTo(User, {
// 创建外键关联 Article.userId => User.id
foreig: "userId"
});
User.hasMany(Article, {
foreig: "userId"
});