[英]Checking if two values match from one to another table
所以我開始用 JavaScript ORM-Sequelize 做一個應用程序。 我有一個名為 Workouts.js 的表,其中練習是名為 Exercises.js 的表中的 ID。
鍛煉.js
ID | 練習一 | 練習二 | 練習三 | 日期 |
---|---|---|---|---|
1個 | 1個 | 2個 | 4個 | 2022-02-21 |
2個 | 4個 | 3個 | 2個 | 2022-02-23 |
3個 | 3個 | 1個 | 1個 | 2022-02-25 |
我有另一個名為 Workout_Volume.js 的表,它具有以下值:
Workout_Volume.js
ID | 鍛煉編號 | 練習編號 | 套 | 代表 | 重量 |
---|---|---|---|---|---|
1個 | 1個 | 1個 | 3個 | 10 | 60 |
2個 | 1個 | 4個 | 4個 | 12 | 40 |
3個 | 3個 | 3個 | 3個 | 15 | 30 |
4個 | 2個 | 4個 | 5個 | 5個 | 80 |
所以我的問題是,在創建和更新 Workout_Volume.js 時,在 Workout_Volume.js 中驗證 workouit_id 和 exercise_id 與 Workouts.js 匹配的正確方法是什么?
鍛煉.js
const workout = (sequelize, DataTypes) => {
const Workout = sequelize.define(
'workouts', {
exercise_one: {
type: DataTypes.INTEGER(), allowNull: false,
references: { model: "exercises", key: "id" },
validate: {
notNull: { msg: "You need to provide exercise_id !" }
}
},
exercise_two: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
exercise_three: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
exercise_four: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
exercise_five: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
exercise_six: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
exercise_seven: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
exercise_eight: {
type: DataTypes.INTEGER(), allowNull: true,
references: { model: "exercises", key: "id" },
},
date: {
type: DataTypes.DATEONLY, allowNull: false,
validate: {
notNull: { msg: "You need to provide date !" }
}
}
},
{
timestamps: false,
freezeTableName: true,
})
Workout.associate = models => {
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_one",
through: 'exerciseOne',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_two",
through: 'exercisTwo',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_three",
through: 'exerciseThree',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_four",
through: 'exerciseFour',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_five",
through: 'exerciseFive',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_six",
through: 'exerciseSix',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_seven",
through: 'exerciseSeven',
targetKey: "id"
})
Workout.belongsToMany(models.exercises, {
foreignKey: "exercise_eight",
through: 'exerciseEight',
targetKey: "id"
})
Workout.belongsTo(models.workout_volume, {
foreignKey:"id",
targetKey: "workout_id",
through: "workout_volume"
})
}
Workout.sync()
return Workout
}
module.exports = workout
Workout_Volume.js
const workoutVolume = (sequelize, DataTypes) => {
const Workout_Volume = sequelize.define(
'workout_volume', {
workout_id: {
type: DataTypes.INTEGER, allowNull: false,
references: { model: "workouts", key: "id" },
validate: {
notNull: { msg: "You need to provide workout_id !" }
}
},
exercise_id: {
type: DataTypes.INTEGER, allowNull: false,
validate: {
notNull: { msg: "You need to provide exercise_id !" }
},
},
sets: {
type: DataTypes.INTEGER, allowNull: false,
validate: {
min: 1,
max: 100,
notNull: { msg: "You need to provide sets!" }
}
},
reps: {
type: DataTypes.INTEGER, allowNull: false,
validate: {
min: 1,
max: 100,
notNull: { msg: "You need to provide reps!" }
}
},
weight: {
type: DataTypes.INTEGER, allowNull: false,
validate: {
min: 0,
max: 1000,
notNull: { msg: "You need to provide sets!" }
}
}
},
{
timestamps: false,
freezeTableName: true,
},
)
Workout_Volume.associate = models => {
Workout_Volume.belongsTo(models.workouts,{
foreignKey: "workout_Id",
as: "workoutId",
targetKey: "id"
})
}
Workout_Volume.sync()
return Workout_Volume
}
module.exports = workoutVolume
我對制作的想法是:
workoutVolume.controller.js
const models = require('../models'); const Workout_Volume = models.workout_volume; exports.getAllWorkouts_Volume = async (req, res) => { try { const workout_volume = await Workout_Volume.findAll() return res.status(200).send(workout_volume) } catch (error) { return res.status(500).send(error) } } exports.getWorkout_Volume = async (req, res) => { try { const workout_volume = await Workout_Volume.findByPk(req.params.id) res.status(200).send(workout_volume) } catch (error) { return res.status(500).send(error) } } exports.createWorkout_Volume = async (req, res) => { try { const exerciseExists = await Workout_Volume.findAll({ where: { exercise_id: req.body.exercise_id, workout_id: req.body.workout_id }, attributes: ["exercise_id", "workout_id"] }) if (exerciseExists.length.== 0) { return res.status(500).send("Exercise already done for this workout.") } await Workout_Volume.create(req.body) return res.status(201).send(req.body) } catch (error) { return res.status(500),send(error) } } exports.updateWorkout_volume = async (req. res) => { try { const workout_volume = await Workout_Volume.findByPk(req.params.id) console.log(req:body) const exerciseExists = await Workout_Volume:findAll({ where. { exercise_id. req,body:exercise_id. workout_id. req,body:workout_id }, attributes. ["exercise_id". "workout_id"] }) if (exerciseExists.length.== 0) { return res.status(500).send("Exercise already done for this workout.") } await workout_volume.update(req.body) return res.status(200).send(req.body) } catch (error) { return res.status(500).send(error) } }
您肯定需要創建一個表/模型Workout_Excercise
而不是在Workout
表/模型中添加列。 這樣你就可以在特定的鍛煉中進行盡可能多的鍛煉。
擁有Workout_Excercise
的第二大好處是您將能夠創建從Workout_Volume
到Workout_Excercise
的正確外鍵:
Workout.hasMany(models.workoutExercises, {
foreignKey: "workoutId",
})
WorkoutExercise.belongsTo(models.exercises, {
foreignKey: "exerciseId",
})
WorkoutExercise.hasMany(models.workout_volume, {
foreignKey: "workoutExerciseId",
})
WorkoutVolume.belongsTo(models.workout_exercise, {
foreignKey: "workoutExerciseId",
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.