findOrCreate with include - sequelize.js(findOrCreate 包含 - sequelize.js)
问题描述
我想创建一个 Tournament(如果不存在)和一个与该锦标赛关联的 Match(如果不存在).
I want to create a Tournament (if not exists), and a Match (if not exists) associated to the Tournament.
let [match, created] = await Match.findOrCreate( {
where: {scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}},
include: [Tournament]
})
如果美国南方男篮"锦标赛已经在数据库中(假设 id 为 1),那么应该使用 TournamentId: 1 创建比赛.如果数据库中已经有比赛 {scoreHome: 97 和 TournamentId: 1}
,则不应创建匹配项.
If the tournament 'USA South Men's Basketball' is already in the db (let's say with id 1), then the match should be created with TournamentId: 1. If there is already a match in the db with {scoreHome: 97 and TournamentId: 1}
, then no match should be created.
let Match = sequelize.define('Match', {
scoreHome: DataTypes.INTEGER,
//...
})
Match.associate = models => {
Match.belongsTo(models.Tournament)
}
编辑这是错误:
[错误:无效值[object Object]]
[Error: Invalid value [object Object]]
这很好用
let match = await Match.create({
scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}
},{include: [Tournament]}
})
推荐答案
我不确定 sequelize 是否会允许这个deep findOrCreate".但是您可以使用两个查询来达到想要的效果,并使用 sequelize 事务来确保两者链接在一起:
I am not sure sequelize will allow this "deep findOrCreate". But you can achieve the wanted effect using two queries, and the sequelize transaction to make sure the two are linked together:
const t = await sequelize.transaction();
try {
const [tournament] = await Tournament.findOrCreate({
where: {name: "USA South Men's Basketball"},
{ transaction: t }
})
const [match] = await Match.findOrCreate({
where: {scoreHome: 97, tournamentId: tournament.id},
{ transaction: t }
})
t.commit()
} catch(err) {
t.rollback()
}
它有点冗长,但这样你就可以确定你在做什么,并且(个人意见)对于在你之后阅读的其他开发人员来说更具可读性.
It is a bit more verbose, but with this you are sure of what you do, and it is also (personal opinion) more readable for another dev reading that after you.
这篇关于findOrCreate 包含 - sequelize.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:findOrCreate 包含 - sequelize.js


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07