Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: Need help with DB schema (ManyToMany, ForeignKey relations)
    primarykey
    data
    text
    <p>I'm new to both Django and MySQL, so I apologize if this ends up being a simple question.</p> <p>I'm working on a project management site. Say we have a <code>Program</code>. Each program has some number of <code>Milestones</code>, which in turn have some number of <code>Tasks</code> that need to be completed for the Milestone to be reached. Milestones will be the same across different Programs, but there is no guarantee that the tasks required for each milestone will stay the same across Programs.</p> <p>Here's an (abbreviated) <code>models.py</code>:</p> <pre class="lang-py prettyprint-override"><code>class Program(models.Model): # ... complete = models.BooleanField() milestones = models.ManyToManyField(Milestone, through='ProgramMilestone') class Milestone(models.Model): # ... class Task(models.Model): # ... complete = models.BooleanField() milestone = models.ForeignKey(Milestone) class ProgramMilestone(models.Model): # ... complete = models.BooleanField() program = models.ForeignKey(Program) milestone = models.ForeignKey(Milestone) </code></pre> <p>I have <code>...through='ProgramMilestone'</code> because <code>Programs</code> should be allowed to share <code>Milestones</code> but have a per-program completion status.</p> <p>The problem with this schema is something like this:</p> <ul> <li>We have two programs, Program 1 and Program 2.</li> <li>Program 1 has a Milestone A, and Program 2 also has a Milestone A.</li> <li>Add a task to Program 1's Milestone A.</li> <li>Unwanted consequence: the task is now also attached to Program 2's Milestone A.</li> </ul> <p>The solution might be to create two "Milestone A"s in the Milestone database: two distinct rows that share the name "Milestone A." However, considering they would only differ in <code>id</code> while being the same conceptual item, this seems like a waste.</p> <p>Another idea might be to require both a <code>ProgramID</code> and a <code>MilestoneID</code> when adding a new <code>Task</code>, but then I'm not sure one might add new Tasks in the Django Admin - that is, how to pass the <code>ProgramID</code> of the Program that the user is currently looking at.</p> <p>How might I adapt my <code>models.py</code> so that Programs can share Milestones while having a per-Program set of Tasks for each Milestone (i.e. avoiding the above scenario)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload