sqlalchemy / sqlalchemy/alembic

IF (NOT) EXISTS for ALTER TABLE sub-commands like constraints etc

Open
#524 7 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

external SQLAlchemy issues feature op directives
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Hello, forgive me if an issue already exists for this, I have seen a related one here but that is several years old and status is unclear.

There doesn't seem to be an option to add IF (NOT) EXISTS or CASCADE flags to queries generated by e.g. op.create_unique_constraint or op.drop_index. Here are some raw queries I've added and directly executed lately:

DROP INDEX ix_host_ip 
CASCADE;

or

ALTER TABLE host 
DROP CONSTRAINT IF EXISTS vuln_occurrence_unique_constraint
CASCADE;

I would like to be able to create these with alembic, for example:

op.drop_index('ix_host_ip', table_name='host', cascade=True)
op.drop_constraint_if_exists('vuln_occurrence_host_ip_fkey', 'vuln_occurrence', type_='foreignkey', cascade=True)

(The syntax could be different to better fit the style of existing commands, if needed.)

I haven't contributed yet but I might be able to work on this issue if it is deemed a worthwhile change to the project.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing op.create_unique_constraint and op.drop_index operation APIs and how they generate ALTER TABLE and DROP INDEX SQL. Check the related issue and current dialect behavior; done means a consistent API for the requested IF (NOT) EXISTS and CASCADE options, with coverage for the affected operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sql, sqlalchemy
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.