diff options
author | Federico Di Gregorio <fog@initd.org> | 2008-04-14 04:13:07 +0000 |
---|---|---|
committer | Federico Di Gregorio <fog@initd.org> | 2008-04-14 04:13:07 +0000 |
commit | 1fddaa856239c01d475301c99131658b5db35f32 (patch) | |
tree | f91a15a11e19f5f1ec9b10337d0b232af6e2c4af /sandbox/trigger-commit-fail.py | |
parent | 23866bc35dd20af7e4c63efe8c4ed06285e4180e (diff) | |
download | psycopg2-1fddaa856239c01d475301c99131658b5db35f32.tar.gz |
Added a couple of files used for tests.
Diffstat (limited to 'sandbox/trigger-commit-fail.py')
-rw-r--r-- | sandbox/trigger-commit-fail.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/sandbox/trigger-commit-fail.py b/sandbox/trigger-commit-fail.py new file mode 100644 index 0000000..98b23ae --- /dev/null +++ b/sandbox/trigger-commit-fail.py @@ -0,0 +1,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 |