<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Gregory &#187; Database</title>
	<atom:link href="http://jagregory.com/writings/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://jagregory.com</link>
	<description>Monkeying with the code</description>
	<lastBuildDate>Wed, 06 Jul 2011 12:48:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>I think you mean a many-to-one sir</title>
		<link>http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/</link>
		<comments>http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 11:08:12 +0000</pubDate>
		<dc:creator>James Gregory</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://blog.jagregory.com/?p=238</guid>
		<description><![CDATA[This is a question that crops up a lot, in various forms, on the Fluent NHibernate and NHibernate Users mailing lists. My one-to-one mapping isn&#8217;t working, what&#8217;s wrong? aka Incorrectly using a one-to-one relationship when you actually need a many-to-one. There&#8217;s a common misunderstanding where people try to use a one-to-one relationship where a many-to-one [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<p>This is a question that crops up <strong>a lot</strong>, in various forms, on the <a href='http://groups.google.com/group/fluent-nhibernate'>Fluent NHibernate</a> and <a href='http://groups.google.com/group/nhusers'>NHibernate Users</a> mailing lists. <em>My one-to-one mapping isn&#8217;t working, what&#8217;s wrong?</em> aka Incorrectly using a one-to-one relationship when you actually need a many-to-one.</p>
</blockquote>
<p>There&#8217;s a common misunderstanding where people try to use a one-to-one relationship where a many-to-one is appropriate. I believe this is because people tend to get tunnel vision when designing their entities, which leads them to make incorrect assumptions. They focus on one entity at a time, and when that has a single entity related to it, they jump to the conclusion it&#8217;s a one-to-one they need; after all, there&#8217;s their current entity (one) and the related entity (to-one). They&#8217;re actually forgetting that there can be multiple instances of their <em>current entity</em>.</p>
<p>There&#8217;s also a big distinction between what&#8217;s possible in the domain, and what&#8217;s possible by design in the database; for example, two businesses may <strong>never</strong> share an address in your application, but in the database there&#8217;s nothing stopping two rows in the Business table from referencing the same address row in the database. This is logic that can be applied on-top of a database design, but there&#8217;s nothing in the underlying pattern to disallow it.</p>
<h2 id='manytoone'>Many-to-one</h2>
<p>Lets have a look at what actually is a many-to-one. Here&#8217;s a small database schema and the related entity.</p>
<pre name="code" class="c-sharp">table Customer (
  Id int primary key,
  Name varchar(100),
  AddressId int foreign key (Address.Id)
)

table Address (
  Id int primary key,
  Number int,
  Street varchar(100)
)

public class Customer
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Address Address { get; set; }
}</pre>
<p>This is a standard many-to-one relationship, many <code>Customers</code> to one <code>Address</code>; the tables are linked by the <code>AddressId</code> key in the <code>Customer</code> table. You can see how people can misinterpret this as a one-to-one relationship when designing the Customer entity; the customer has one address, so it must be a one-to-one. People forget about this scenario:</p>
<table class="db-table">
<caption>Customer</caption>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>AddressId</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Plumbers</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>Joiners</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Roofers</td>
<td>1</td>
</tr>
</tbody>
</table>
<table class="db-table">
<caption>Address</caption>
<thead>
<tr>
<th>Id</th>
<th>Number</th>
<th>Street</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>23</td>
<td>Baker St.</td>
</tr>
<tr>
<td>2</td>
<td>47</td>
<td>Jefferson Ave.</td>
</tr>
</tbody>
</table>
<p>That is, the first and third customer both reference the first address.</p>
<h2 id='onetoone'>One-to-one</h2>
<p><strong>So what actually is a one-to-one relationship then?</strong> A one-to-one is a relationship between two tables that share a mutually exclusive primary key value.</p>
<pre name="code" class="c-sharp">table Customer (
  Id int primary key,
  Name varchar(100),
)

table Address (
  Id int primary key,
  Number int,
  Street varchar(100)
)

public class Customer
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Address Address { get; set; }
}</pre>
<p>You can see in this design <code>Customer</code> has no direct reference to <code>Address</code>, the two tables share a primary key value; so there would be a record in <code>Customer</code> with a primary key of <code>1</code>, and a record in <code>Address</code> that also has a primary key of <code>1</code>. It&#8217;s fairly common to have the primary key on the second table (<code>Address</code> in our case) be manually inserted on creation of a record in the first, so it may only be the first table that has a true auto-incrementing primary key.</p>
<p>It&#8217;s also noticeable that both examples have exactly the same class design, this probably contributes to the confusion too, as it&#8217;s not immediately clear from the class what kind of relationship it is.</p>
<p>So just remember this: <strong>if you think you&#8217;re mapping a one-to-one, you probably aren&#8217;t!</strong> It&#8217;s pretty uncommon to find a one-to-one relationship in a properly designed schema, 90% of the time it&#8217;ll be a many-to-one you need.</p>
]]></content:encoded>
			<wfw:commentRss>http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
