Note that there are some explanatory texts on larger screens.

plurals
  1. PODefine One to Many Relationships with SQL
    primarykey
    data
    text
    <p>I'm looking for a way to set up a one to many relationship between 2 tables. The table structures is explained below but I've tried to leave everything off that has nothing to do with the problem.</p> <p>Table <code>objects</code> has 1 column called <code>uuid</code>.</p> <p>Table <code>contents</code> has 3 columns called <code>content</code>, <code>object_uuid</code> and <code>timestamp</code>.</p> <p>The basic idea is to insert a row into <code>objects</code> and get a new <code>uuid</code> from the database. This <code>uuid</code> is then used stored for every row in <code>contents</code> to associate contents with objects.</p> <p>Now I'm trying to use the database to enforce that:</p> <ul> <li>Each row in <code>contents</code> references a row in <code>objects</code> (a foreign key should do)</li> <li>No row in <code>objects</code> exists without at least a <code>row</code> in <code>contents</code></li> </ul> <p>These constraints should be enforced on commit of transactions.</p> <p>Ordinary triggers can't help probably because when a row in the <code>objects</code> table is written, there can't be a row in <code>contents</code> yet. Postgres does have so called <code>constraint triggers</code> that can be deferred until the end of the transaction. It would be possible to use those but they seem to be some sort of internal construct not intended for everyday use.</p> <p>Ideas or solutions should be standard SQL (preferred) or work with Postgres (version does not matter). Thanks for any input.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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