簡體   English   中英

檢查兩個值是否從一個表匹配到另一個表

[英]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

我對制作的想法是:

  • 將 exercise_id 關聯到 Workouts.js 中的每個鍛煉值。 問題是練習是 8,我認為項目會變得更加硬編碼;
  • 不要進行關聯,而是進行 for 循環,我將在其中檢查請求的 workout_id 和 exercises_id 是否與 Workout.js 值匹配; 或者,如果有更好的方法來構建表格,那么它更有意義
  • 或者是否有更好的方法來構建表或其他東西。

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_id,但可以創建不在 Workout.js 中的 exercise_id。 另外,當我嘗試更新它時,長度已經是 !== 0

您肯定需要創建一個表/模型Workout_Excercise而不是在Workout表/模型中添加列。 這樣你就可以在特定的鍛煉中進行盡可能多的鍛煉。
擁有Workout_Excercise的第二大好處是您將能夠創建從Workout_VolumeWorkout_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM