findOrCreate with include - sequelize.js

天涯浪子 提交于 2019-12-07 15:22:44

问题


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]
})

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)
  }

EDIT Here is the error:

[Error: Invalid value [object Object]]

This works fine

let match = await Match.create({
    scoreHome: 97, Tournament: {name: "USA South Men's Basketball"}
  },{include: [Tournament]}
})

来源:https://stackoverflow.com/questions/45530818/findorcreate-with-include-sequelize-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!