关联关联插入

时间:2019-04-13 18:09:44

标签: sqlite sequelize.js

sqlite中将sequelizenodejs一起使用

以下是我的模型定义

const Tournament = sequelize.define('tournament', {
  tournamentName: {
    type: Sequelize.STRING(30),
    field: 'tournament_name',
    allowNull: false
  }
})


const Series = sequelize.define('series', {
  seriesName: {
    type: Sequelize.STRING(100),
    field: 'series_name',
    allowNull: false
  }
})


const Match = sequelize.define('match', {
  team1: {
    type: Sequelize.INTEGER,
    field: 'team1',
    allowNull: false
  },
  team2: {
    type: Sequelize.INTEGER,
    field: 'team2',
    allowNull: false
  },
  matchName: {
    type: Sequelize.STRING(100),
    field: 'match_name',
    allowNull: false
  },
  matchDate: {
    type: Sequelize.DATE,
    field: 'match_date',
    allowNull: false
  }
})


// Associations

Series.belongsTo(Tournament);
Match.belongsTo(Series);

// Create Tournament

sequelize.sync({
  force: true
}).then(() => {
  Tournament.create({
    tournamentName: 'ICC Cricket World Cup'
  }).then((tournament) => {

// Need to create a series with name 'World Cup 2019 - England & Wales' and the tournament is the one created in the method

  })
})

不确定如何创建新系列并包含比赛对象。

0 个答案:

没有答案