<p>I am intentionally leaving this quite vague at first.  I'm looking for discussion and what issues are important more than I'm looking for hard answers.</p>

<p>I'm in the middle of designing an app that does something like portfolio management.  The design I have so far is</p>

<ul>
<li>Problem: a problem that needs to be solved</li>
<li>Solution: a proposed solution to one or more problems</li>
<li>Relationship: a relationship among two problems, two solutions, or a problem and a solution.  Further broken down into:

<ul>
<li>Parent-child - some sort of categorization / tree hierarchy</li>
<li>Overlap - the degree to which two solutions or two problems really address the same concept</li>
<li>Addresses - the degree to which a problem addresses a solution</li>
</ul></li>
</ul>

<p>My question is about the temporal nature of these things.  Problems crop up, then fade.  Solutions have an expected resolution date, but that might be modified as they are developed.  The degree of a relationship might change over time as problems and solutions evolve.</p>

<p>So, the question: what is the best design for versioning of these things so I can get both a current and an historical perspective of my portfolio?</p>

<p><em>Later: perhaps I should make this a more specific question, though @Eric Beard's answer is worth an up.</em></p>

<p>I've considered three database designs.  I'll enough of each to show their drawbacks.  My question is: which to pick, or can you think of something better?</p>

<h2>1: Problems (and separately, Solutions) are self-referential in versioning.</h2>

<pre><code>table problems
  int id | string name | text description | datetime created_at | int previous_version_id

  foreign key previous_version_id -&gt; problems.id
</code></pre>

<p>This is problematic because every time I want a new version, I have to duplicate the entire row, including that long <code>description</code> column.</p>

<h2>2: Create a new Relationship type: Version.</h2>

<pre><code>table problems
  int id | string name | text description | datetime created_at
</code></pre>

<p>This simply moves the relationship from the Problems and Solutions tables into the Relationships table.  Same duplication problem, but perhaps a little "cleaner" since I already have an abstract Relationship concept.</p>

<h2>3: Use a more Subversion-like structure; move all Problem and Solution attributes into a separate table and version them.</h2>

<pre><code>table problems
  int id

table attributes
  int id | int thing_id | string thing_type | string name | string value | datetime created_at | int previous_version_id

  foreign key (thing_id, thing_type) -&gt; problems.id or solutions.id
  foreign key previous_version_id -&gt; attributes.id
</code></pre>

<p>This means that to load the current version of a Problem or Solution I have to fetch all versions of the attribute, sort them by date and then use the most current.  That might not be terrible.  What seems really bad to me is that I can't type-check these attributes in the database.  That <code>value</code> column has to be free-text.  I can make the <code>name</code> column a reference into a separate <code>attribute_names</code> table that has a <code>type</code> column, but that doesn't <em>force</em> the correct type in the <code>attributes</code> table.</p>

<p><em>later still: response to @Eric Beard's comments about multi-table foreign keys:</em></p>

<p>Alas, what I've described is simplistic: there are only two types of Things (Problems and Solutions).  I actually have about 9 or 10 different types of Things, so I'd have 9 or 10 columns of foreign keys under your strategy.  I wanted to use single-table inheritance, but the Things have so little in common that it would be <em>extremely</em> wasteful to do combine them into one table.</p>

[{'answer_id': 11694, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 2, 'selected': True, 'text': '<p>Hmm, sounds kind of like this site...</p>\n\n<p>As far as a database design would go, a versioning system kind of like SVN, where you never actually do any updates, just inserts (with a version number) when things change, might be what you need.  This is called MVCC, Multi-Value Concurrency Control.  A wiki is another good example of this.</p>\n'}, {'answer_id': 12231, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 1, 'selected': False, 'text': '<p>@Gaius</p>\n\n<pre><code>foreign key (thing_id, thing_type) -&gt; problems.id or solutions.id\n</code></pre>\n\n<p>Be careful with these kinds of "multidirectional" foreign keys.  My experience has shown that query performance suffers dramatically when your join condition has to check the type before figuring out which table to join on.  It doesn\'t seem as elegant but nullable </p>\n\n<pre><code>problem_id and solution_id \n</code></pre>\n\n<p>will work much better.</p>\n\n<p>Of course, query performance will also suffer with an MVCC design when you have to add the check to get the latest version of a record.  The tradeoff is that you never have to worry about contention with updates.</p>\n'}, {'answer_id': 12278, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 0, 'selected': False, 'text': "<p>I suppose there's</p>\n\n<h2>Option 4: the hybrid</h2>\n\n<p>Move the common Thing attributes into a single-inheritance table, then add an <code>custom_attributes</code> table.  This makes foreign-keys simpler, reduces duplication, and allows flexibility.  It doesn't solve the problems of type-safety for the additional attributes.  It also adds a little complexity since there are two ways for a Thing to have an attribute now.</p>\n\n<p>If <code>description</code> and other large fields stay in the Things table, though, it also doesn't solve the duplication-space problem.</p>\n\n<pre><code>table things\n  int id | int type | string name | text description | datetime created_at | other common fields...\n  foreign key type -&gt; thing_types.id\n\ntable custom_attributes\n  int id | int thing_id | string name | string value\n  foreign key thing_id -&gt; things.id\n</code></pre>\n"}, {'answer_id': 214780, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>How do you think about this:</p>\n\n<p>table problems<br>\n  int id | string name | text description | datetime created_at </p>\n\n<p>table problems_revisions<br>\n  int revision | int id | string name | text description | datetime created_at<br>\n  foreign key id -> problems.id</p>\n\n<p>Before updates you have to perform an additional insert in the revision table. This additional insert is fast, however, this is what you have to pay for </p>\n\n<ol>\n<li>efficient access to the current version - select problems as usual</li>\n<li>a schema that is intuitive and close to the reality you want to model</li>\n<li>joins between tables in your schema keep efficient</li>\n<li>using a revision number per busines transaction you can do versioning over table records like SVN does over files.</li>\n</ol>\n'}, {'answer_id': 252983, 'author': 'WW.', 'author_id': 14663, 'author_profile': 'https://Stackoverflow.com/users/14663', 'pm_score': 0, 'selected': False, 'text': "<p>It's a good idea to choose a data structure that makes common questions that you ask of the model easy to answer.  It's most likely that you're interested in the current position most of the time.  On occasion, you will want to drill into the history for particular problems and solutions.</p>\n\n<p>I would have tables for problem, solution, and relationship that represent the current position.  There would also be a <code>problem_history</code>, <code>solution_history</code>, etc table.  These would be child tables of problem but also contain extra columns for <code>VersionNumber</code> and <code>EffectiveDate</code>.  The key would be (<code>ProblemId</code>, <code>VersionNumber</code>).</p>\n\n<p>When you update a problem, you would write the old values into the <code>problem_history</code> table.  Point in time queries are therefore possible as you can pick out the <code>problem_history</code> record that is valid as-at a particular date.</p>\n\n<p>Where I've done this before, I have also created a view to UNION <code>problem</code> and <code>problem_history</code> as this is sometimes useful in various queries.</p>\n\n<p>Option 1 makes it difficult to query the current situation, as all your historic data is mixed in with your current data.</p>\n\n<p>Option 3 is going to be bad for query performance and nasty to code against as you'll be accessing lots of rows for what should just be a simple query.</p>\n"}]
['https://Stackoverflow.com/questions/11689', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']