title: Looking for a right EAV structure based on jsonb
body: <p>I wondering what will be the right approach to build EAV on jsonb.
  I have <code>Attribute</code> -> <code>Values</code> tables as like in standard EAV.</p>
  
  <pre><code>CREATE TABLE attribute_values
  (
    id           INTEGER,
    attribute_id INTEGER,
    value        VARCHAR(255)
  );
  
  CREATE TABLE attributes
  (
    id   INTEGER,
    name VARCHAR(255)
  );
  </code></pre>
  
  <p>Values will saved in <code>attributes</code> filed of <code>Entity</code></p>
  
  <pre><code> CREATE TABLE entity
      (
        id   INTEGER,
        title TEXT,
        attributes JSONB
      );
  </code></pre>
  
  <p>Tables <code>Attribute</code> created to control duplicate attributes  their types and better determine what it's a attribute is. For example to avoid: <code>{weight: 100}</code> and <code>{Weight: 100}</code> or <code>{weigh: 100}</code>. <code>Values</code> for work with unique values and contain avaliable list of values like color (green, red, white etc.) Values can be preloaded and using for faseted search.</p>
  
  <p>I see several options:</p>
  
  <p><strong>1.</strong> Store format like </p>
  
  <pre><code>[{"attribute_id":1, "value":5},{"attribute_id":1, value:"text"}] 
  </code></pre>
  
  <p>where <code>value_id</code> will be <code>custom value</code> like text or <code>id</code> from <code>Values</code> table. But I can't understand how to build indexing on this format, for example if <code>Attribute 10</code> will <code>integer</code> </p>
  
  <p><strong>2.</strong> leave only <code>Attribute</code> table (for controlling attribute <code>name</code>) and store data like: </p>
  
  <pre><code>{"price": 105, "weight": 100, "color": "white"}
  </code></pre>
  
  <p>. This approach much better for indexing</p>
  
  <pre><code>CREATE INDEX entity_index ON entity (((attributes -&gt;&gt; 'price')::int)); 
  </code></pre>
  
  <p>but I will have problem with translation of text property and controlling of unique values. Also I can't add additional key like in option <code>1</code>: <code>{"attribute_id":1, "value":5, "values": []}</code></p>
  
  <p>What will be the best approach to store extra field with unique control (for unique attributes) and with the opportunity to indexing.</p>