目前,我有两个表,Exercise
和 WorkoutPlan
。这些将具有多对多的关系。
Exercise
- ID
- Name
...
WorkoutPlan
- ID
- Name
- Exercises (Many to Many with Exercise through WorkoutPlanExercise)
...
在这个多对多关系表中,我需要存储一些集合
的信息,比如min_rest
, max_rest
, min_repetitions
和 max_repetitions
。
我卡住的地方是,试图找出最好的解决方案来做到这一点。我的第一个解决方案是使用另一个表 (WorkoutPlanExerciseSet
),它与多对多表 (WorkoutPlanExercise
) 具有多对一关系,如如下所示。
WorkoutPlanExercise
- ID
- ExerciseID
- WorkoutPlanID
- Sets (One to Many with WorkoutPlanExerciseSet)
WorkoutPlanExerciseSet
- WorkoutPlanExerciseID
- MinRepititions
- MaxRepititions
- MinRest
- MaxRest
我的第二个解决方案 是将有关练习集的所有信息存储在多对多关系表 (WorkoutPlanExercise
) 的一行中。例如:
WorkoutPlanExercise
ID ExerciseID WorkoutPlanID Sets Repititions Rest
1 1 1 3 10-12, 10-10, 12-12 90-120, 60-90, 30-30
请注意,休息时间和重复次数都可以是一个范围或单个数字。对于第二种解决方案,我想我会创建一个自定义 Django 表单字段。
哪个更好?是不是以前的数据库设计不好?后者是糟糕的应用程序设计吗?
如果有什么不同,我希望能够以用户友好的方式轻松显示信息,例如:
Example Workout Plan
Exercise Sets Repetitions Rest
Pull Ups 3 10 - 12 90 - 120
8 - 10 30
6 - 8 30
最佳答案
我想是第二个。阅读自定义 through
字段 here .
更新:查看评论。
更新 2、3:
其实两个都挺好的。这取决于您希望如何处理存储在 Repetitions
和 Rest
字段中的数据。如果您想对数据进行大量操作和计算,例如。 G。计算 WorkoutPlan 的总休息时间或 Exercise 的总重复次数,然后使用前一种方法会稍微容易一些。
更新 4:
在一个字段中存储与 CSV 相同类型的数据是个坏主意。如果您将来必须更改架构,您将获得很多乐趣。使用第一种方法。还有 link1 , link2 .
关于python - 联结表(多对多)或自定义字段类型上的多对一关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122166/