Reputation: 2451
In a web2py application, I've got a primary postgresql database (db1), and I've set up a separate postgresql database (db1_archive) used for archiving several of the tables from db1. This functionality is implemented in a module with the following:
class db_archives():
def __init__(self, request, response, db1_archive, db1, auth):
...
auth.enable_record_versioning(
archive_db = db1_archive,
tables = [db1.auth_user, db1.table1, db1.table2, ...]
archive_names='archive_%(tablename)s'
)
When something is deleted from the relevant tables in db1, it should automatically be archived in db1_archive. However, upon deletion, the following psycopg2.IntegrityError is produced instead:
insert or update on table "archive_table1" violates foreign key constraint "archive_table1_created_by_fkey" DETAIL: Key (created_by)=(9) is > not present in table "auth_user"
I guess what's happening is that the archive DB is not finding a user with an ID of 9. The 'created_by' field has the following constraint:
FOREIGN KEY (created_by) REFERENCES auth_user(id) ON DELETE CASCADE
I'd like to remove all constraints from the archive db. I think this can be done with web2py's [database_name].executesql()
function. However, I've tried a few things such as db1_archive.executesql('SET FOREIGN_KEY_CHECKS=0;')
, and only ended up with error messages. Can anyone advise me on some SQL or web2py codes I could try to fix the situation? Thanks.
Upvotes: 1
Views: 486
Reputation: 26464
What you need to do is build a list of the constraint names and then walk through them in your code and:
alter table [tablename] drop constraint [fkeyname];
If you need to pull this info from the system catalogs, see the pg_constraint
table.
Please note that alter table cannot be parameterized so you will have to assemble the command as a string with proper escaping and run it.
Upvotes: 1