Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This looks like a simple m:n relationship with a two-part key. Model it relationally with a joint table. To give you the idea, assuming there can only be one <code>(post, attachment)</code> pair per c-ordinate location, the SQL phrasing would be:</p> <pre><code>CREATE TABLE post ( post_id serial primary key, blah_other_data text ); CREATE TABLE attachment ( attachment_id serial primary key, blah_other_data text ); CREATE TABLE post_attachment ( post_id integer not null REFERENCES post(post_id), attachment_id integer not null REFERENCES attachment(attachment_id), coordinate_x integer not null, coordinate_y integer not null, PRIMARY KEY (coordinate_x, coordinate_y) ); </code></pre> <p>I'm sure you can translate that back into Rails models and queries.</p> <p>If there can be multiple <code>(post,attachment)</code> pairs per co-ordinate location then you must adjust the <code>PRIMARY KEY</code> of <code>post_attachments</code> to permit that by adding the co-ordinate columns to the key.</p> <p>Demo data setup:</p> <pre><code>INSERT INTO post (blah_other_data) VALUES ('Post1'),('Post2'),('Post3'); INSERT INTO attachment(blah_other_data) VALUES ('Attachment1'),('Attachment2'),('Attachment3'); INSERT INTO post_attachment (post_id, attachment_id, coordinate_x, coordinate_y) SELECT post_id, attachment_id, x, y FROM (VALUES ('Post1', 100,200, 'Attachment1'), ('Post1', 400,400, 'Attachment2'), ('Post1', 200,500, 'Attachment3'), ('Post2', 150,310, 'Attachment1'), ('Post3', 50,710, 'Attachment1'), ('Post1', 430,430, 'Attachment2') ) rows(post_data,x,y,attachment_data) INNER JOIN post ON (post.blah_other_data = rows.post_data) INNER JOIN attachment ON (attachment.blah_other_data = rows.attachment_data); </code></pre> <p>Access the data with joins:</p> <pre><code>SELECT p.post_id, p.blah_other_data AS post_data, a.attachment_id, a.blah_other_data AS attachment_data, c.coordinate_x, c.coordinate_y FROM post_attachment c INNER JOIN post p ON (c.post_id = p.post_id) INNER JOIN attachment a ON (c.attachment_id = a.attachment_id); post_id | post_data | attachment_id | attachment_data | coordinate_x | coordinate_y ---------+-----------+---------------+-----------------+--------------+-------------- 3 | Post3 | 1 | Attachment1 | 50 | 710 2 | Post2 | 1 | Attachment1 | 150 | 310 1 | Post1 | 1 | Attachment1 | 100 | 200 1 | Post1 | 2 | Attachment2 | 400 | 400 1 | Post1 | 2 | Attachment2 | 430 | 430 1 | Post1 | 3 | Attachment3 | 200 | 500 (6 rows) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload