-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathlinks.py
60 lines (52 loc) · 1.56 KB
/
links.py
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
from db import connection as db_conn
def add_foreign_key_column(
conn,
column_name,
referrer_table_oid,
referent_table_oid,
unique_link=False
):
"""
Creates a Many-to-One or One-to-One link.
Args:
conn: psycopg3 connection object.
column_name: Name of the new column to be created in the referrer
table.
referrer_table_oid: The OID of the referrer table.
referent_table_oid: The OID of the referent table.
unique_link: Whether to make the link one-to-one
instead of many-to-one.
"""
db_conn.exec_msar_func(
conn,
'add_foreign_key_column',
column_name,
referrer_table_oid,
referent_table_oid,
unique_link
)
def add_mapping_table(
conn,
schema_oid,
table_name,
mapping_columns,
):
"""
Add a mapping table to give a many-to-many link between referents.
Args:
conn: psycopg3 connection object.
schema_oid: The OID of the schema for the mapping table.
table_name: The name for the new mapping table.
mapping_columns: A list of dictionaries giving the foreign key
columns to create in the mapping table.
The elements of the mapping_columns list must have the form
{"column_name": <str>, "referent_table_oid": <int>}
"""
db_conn.exec_msar_func(
conn,
'add_mapping_table',
schema_oid,
table_name,
json.dumps(mapping_columns)
)