title: SQL-Association table with any level of hierarchy
body: <p>I have a <code>product</code> table with following schema</p>
  
  <pre><code>     Column     |           Type           |
  ----------------+--------------------------+
   id             | integer                  |
   name           | character varying(255)   |
  </code></pre>
  
  <p>and a <code>location</code> table with schema</p>
  
  <pre><code>       Column     |           Type           |
    ----------------+--------------------------+
     id             | integer                  |
     country        | character varying(255)   |
     state          | character varying(255)   |
     region         | character varying(255)   |
     city           | character varying(255)   |
  </code></pre>
  
  <p>I need to have many-to-many relation between the two tables and <code>product</code> can be associated with any level of location in <code>location</code> table. For example a product can be sold in a whole country or one specific region or city.</p>
  
  <p>I can make entries for all the locations in mapping table if user chooses location at country level(example. if user chooses country US, an entry for all the cities in the US can be made in mapping table) but that would create large number of mapping rows.</p>
  
  <p>What would be the best to map the two table in a way that I can choose any level of location?</p>