<small id='tRdpA'></small><noframes id='tRdpA'>

    1. <legend id='tRdpA'><style id='tRdpA'><dir id='tRdpA'><q id='tRdpA'></q></dir></style></legend>
      <tfoot id='tRdpA'></tfoot>
        <i id='tRdpA'><tr id='tRdpA'><dt id='tRdpA'><q id='tRdpA'><span id='tRdpA'><b id='tRdpA'><form id='tRdpA'><ins id='tRdpA'></ins><ul id='tRdpA'></ul><sub id='tRdpA'></sub></form><legend id='tRdpA'></legend><bdo id='tRdpA'><pre id='tRdpA'><center id='tRdpA'></center></pre></bdo></b><th id='tRdpA'></th></span></q></dt></tr></i><div id='tRdpA'><tfoot id='tRdpA'></tfoot><dl id='tRdpA'><fieldset id='tRdpA'></fieldset></dl></div>
          <bdo id='tRdpA'></bdo><ul id='tRdpA'></ul>
      1. findOrCreate 包含 - sequelize.js

        时间:2023-09-03

        <small id='bpNHS'></small><noframes id='bpNHS'>

        1. <i id='bpNHS'><tr id='bpNHS'><dt id='bpNHS'><q id='bpNHS'><span id='bpNHS'><b id='bpNHS'><form id='bpNHS'><ins id='bpNHS'></ins><ul id='bpNHS'></ul><sub id='bpNHS'></sub></form><legend id='bpNHS'></legend><bdo id='bpNHS'><pre id='bpNHS'><center id='bpNHS'></center></pre></bdo></b><th id='bpNHS'></th></span></q></dt></tr></i><div id='bpNHS'><tfoot id='bpNHS'></tfoot><dl id='bpNHS'><fieldset id='bpNHS'></fieldset></dl></div>
            <tbody id='bpNHS'></tbody>

                <legend id='bpNHS'><style id='bpNHS'><dir id='bpNHS'><q id='bpNHS'></q></dir></style></legend>
                <tfoot id='bpNHS'></tfoot>
                • <bdo id='bpNHS'></bdo><ul id='bpNHS'></ul>
                  本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 sequelize.js 中使用 build 方法时是否需要验证、清理或转义数据 下一篇:续集关联 - 请改用 promise 样式

                  相关文章

                  <i id='mEVSa'><tr id='mEVSa'><dt id='mEVSa'><q id='mEVSa'><span id='mEVSa'><b id='mEVSa'><form id='mEVSa'><ins id='mEVSa'></ins><ul id='mEVSa'></ul><sub id='mEVSa'></sub></form><legend id='mEVSa'></legend><bdo id='mEVSa'><pre id='mEVSa'><center id='mEVSa'></center></pre></bdo></b><th id='mEVSa'></th></span></q></dt></tr></i><div id='mEVSa'><tfoot id='mEVSa'></tfoot><dl id='mEVSa'><fieldset id='mEVSa'></fieldset></dl></div>

                      <small id='mEVSa'></small><noframes id='mEVSa'>

                      • <bdo id='mEVSa'></bdo><ul id='mEVSa'></ul>
                    1. <tfoot id='mEVSa'></tfoot><legend id='mEVSa'><style id='mEVSa'><dir id='mEVSa'><q id='mEVSa'></q></dir></style></legend>