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

Do not create FK if exists #37

Open
tinogis opened this issue Nov 13, 2023 · 2 comments
Open

Do not create FK if exists #37

tinogis opened this issue Nov 13, 2023 · 2 comments
Assignees
Labels

Comments

@tinogis
Copy link
Member

tinogis commented Nov 13, 2023

If we are trying to create a fk constraint and it exists, fails.

We can put a constraint existence test like:

select constraint_name from information_schema.constraint_column_usage where table_name = [t_name] and constraint_name = [c_name]

or drop and create:

ALTER TABLE [t_name] DROP CONSTRAINT IF EXISTS [c_name]

in https://github.com/gisce/oopgrade/blob/661689373ae565a68f0d5400307a383b244d1109/oopgrade/oopgrade.py#L297C28-L297C29

@ecarreras @polsala

@tinogis tinogis self-assigned this Nov 13, 2023
@tinogis tinogis added the bug label Nov 13, 2023
@ecarreras
Copy link
Member

The intention of add_columns_fk is only add not check if it must be added, for that exists get_foreign_keys which should be called in the script to check if foreign_key must be created or not

@ecarreras
Copy link
Member

ecarreras commented Nov 13, 2023

We can use get_foreign_keys in the method to check if foreign exists or not with the following order:

  • Get the foreign keys
  • If exists and is with the same foreign key and foreign table don't do anything
  • If exists but is with other foreign key or other foreign table raise an exception
  • If the foreign key doesn't exist create it.

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

No branches or pull requests

2 participants