Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing ON UPDATE and ON DELETE triggers for constraints #9

Open
oojacoboo opened this issue Jun 13, 2022 · 1 comment
Open

Missing ON UPDATE and ON DELETE triggers for constraints #9

oojacoboo opened this issue Jun 13, 2022 · 1 comment

Comments

@oojacoboo
Copy link
Contributor

It appears that, when the foreign keys are added back, the constraints are missing the ON UPDATE and ON DELETE triggers/events.

From the code below that seems to be the case. However, I do see some FK constraints with the triggers. I'm assuming these are copied over somehow though and not actually generated.

    fk_enable = "\n".join(
        [
            cleandoc(
                f"""
            ALTER TABLE {fk['schema']}.{fk['local_table']}
            ADD CONSTRAINT {fk['constraint']} FOREIGN KEY ({fk['local_column']})
            REFERENCES {fk['schema']}.{fk['foreign_table']} ({fk['foreign_column']});
        """
            )
            for fk in get_foreign_keys(host, port, database, schema, table, user, password)
        ]
    )
@oojacoboo
Copy link
Contributor Author

Here is a pure SQL way to build out the ALTER statements for restoring the constraints. You can build the drops, similarly:

DROP

SELECT 'ALTER TABLE "'||nspname||'"."'||relname||'" DROP CONSTRAINT "'||conname||'";'
FROM pg_constraint
INNER JOIN pg_class ON conrelid=pg_class.oid
INNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
ORDER BY CASE WHEN contype='f' THEN 0 ELSE 1 END,contype,nspname,relname,conname;

ADD

SELECT 'ALTER TABLE "'||nspname||'"."'||relname||'" ADD CONSTRAINT "'||conname||'" '|| pg_get_constraintdef(pg_constraint.oid)||';'
FROM pg_constraint
INNER JOIN pg_class ON conrelid=pg_class.oid
INNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
ORDER BY CASE WHEN contype='f' THEN 0 ELSE 1 END DESC,contype DESC,nspname DESC,relname DESC,conname DESC;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant