summaryrefslogtreecommitdiff
path: root/sandbox/trigger-commit-fail.py
blob: 0655c2f246dd78974637625cb0d789aa90e83501 (plain)
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
import psycopg2
import traceback

# Change the table here to something the user can create tables in ...
db = psycopg2.connect('dbname=test')

cursor = db.cursor()

print('Creating tables and sample data')

cursor.execute('''
  CREATE TEMPORARY TABLE foo (
    id int PRIMARY KEY
  )''')
cursor.execute('''
  CREATE TEMPORARY TABLE bar (
    id int PRIMARY KEY,
    foo_id int,
    CONSTRAINT bar_foo_fk FOREIGN KEY (foo_id) REFERENCES foo(id) DEFERRABLE
  )''')
cursor.execute('INSERT INTO foo VALUES (1)')
cursor.execute('INSERT INTO bar VALUES (1, 1)')

db.commit()

print('Deferring constraint and breaking referential integrity')
cursor.execute('SET CONSTRAINTS bar_foo_fk DEFERRED')
cursor.execute('UPDATE bar SET foo_id = 42 WHERE id = 1')

print('Committing (this should fail)')
try:
    db.commit()
except:
    traceback.print_exc()

print('Rolling back connection')
db.rollback()

print('Running a trivial query')
try:
    cursor.execute('SELECT TRUE')
except:
    traceback.print_exc()
print('db.closed:', db.closed)