title: Polymorphic Associations or something easier
body: <p>This is my first post and I can't seem to find the answer anywhere....</p>
  
  <p>I have a database that has multiple companies,each company has multiple locations.
  I'm running into problems trying to define the contacts. Some contacts need to be global and available
  at any location....some contacts only need to exist for one location. In the contact_info table below 
  we specify the visibility of the contact (company or location). However the location needs to choose its primary contact.
  That leaves a FK from contact -> location and from location -> contact.</p>
  
  <p>I know there is another table involved but I can't seem to conceptualize it.</p>
  
  <pre><code>CREATE TABLE `company_info` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL DEFAULT '',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `name` (`name`));
  
  CREATE TABLE `location_info` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `company_info` INT NOT NULL DEFAULT -1,
  `name` VARCHAR(100) NOT NULL DEFAULT '',
  `primary_contact_id` INT NOT NULL DEFAULT -1,
  PRIMARY KEY(`id`),
  UNIQUE KEY(`company_id`,`name`),
  FOREIGN KEY (company_id) REFERENCES company_info(id)
  FOREIGN KEY (primary_contact_id) REFERENCES contact_info(id));
  
  CREATE TABLE `contact_info` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `company_id` INT
  `location_id` INT,
  `type` ENUM('Company','Location') NOT NULL DEFAULT 'Company',
  `first_name` VARCHAR(50) NOT NULL DEFAULT '',
  `last_name` VARCHAR(50) NOT NULL DEFAULT '',
  PRIMARY KEY(`id`),
  UNIQUE KEY(`id`,`company_id`,`location_id`),
  FOREIGN KEY (location_id) REFERENCES location_info(id),
  FOREIGN KEY (company_id) REFERENCES company_info(id)
  </code></pre>