-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsurrogate_table.sql.jinja2
More file actions
26 lines (25 loc) · 958 Bytes
/
surrogate_table.sql.jinja2
File metadata and controls
26 lines (25 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- Surrogate key table
{#
We cannot use gist temporal indexes here in place of a usual type of
ID primary key since foreign keys rely on a unique index being
present.
#}
CREATE TABLE IF NOT EXISTS "{{table}}" (
{%- for col in table_.pk %}
{{col.name}} {{col.type if col.fk else col.serial_type}} not null {%- if col.fk %} references "{{col.fk.table}}"("{{col.fk.ref_id}}") {%- endif %},
{%- endfor %}
PRIMARY KEY ("{{table_.pk[0].name}}"{%- for c in table_.pk[1:] %},"{{c.name}}"{% endfor %})
);
CREATE TABLE IF NOT EXISTS "{{table}}__valid" (
metadata integer not null
{%- for col in table_.pk %}
,{{col.name}} {{col.type}} not null {%- if col.fk %} references "{{col.fk.table}}"("{{col.fk.ref_id}}") {%- endif %}
{%- endfor %}
,EXCLUDE USING gist (
{%- for col in table_.pk %}
"{{col.name}}" with =,
{%- endfor %}
transaction_time with &&,
valid_time with &&
)
) INHERITS (transaction_valid_{{table_.valid_type}});