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

"drop constraint if exists" unavailable #2167

Open
andrei-pmbcn opened this issue Jul 21, 2017 · 13 comments · May be fixed by #6069
Open

"drop constraint if exists" unavailable #2167

andrei-pmbcn opened this issue Jul 21, 2017 · 13 comments · May be fixed by #6069

Comments

@andrei-pmbcn
Copy link

Based on the documentation, there seems to be no way of including an "IF EXISTS" condition when dropping constraints such as foreign keys, so "ALTER TABLE 'x' DROP CONSTRAINT IF EXISTS 'x_y_foreign' ", for instance, can only be done using a raw string. Moreover, a "table.raw" method does not exist, so one has to make the entire ALTER TABLE statement raw. This can be a pretty awkward problem when running scripts that generate schemas. A convoluted workaround is available using whereExists, but it would be ideal if table.foreign etc had an argument to include the "IF EXISTS" condition.

@elhigu
Copy link
Member

elhigu commented Jul 23, 2017

@andrei-pmbcn have you any API in mind how it would work consistently with other knex schema building APIs? If this is a feature request, please write it as one and add some proposal how the feature should work.

@wubzz
Copy link
Member

wubzz commented Jul 24, 2017

For consistency...

QueryBuilder API

.dropTable()
.dropTableIfExists()

SchemaBuilder API

.dropForeign()
.dropForeignIfExists()
.dropUnique()
.dropUniqueIfExists()
.dropPrimary()
.dropPrimaryIfExists()

@elhigu
Copy link
Member

elhigu commented Jul 24, 2017

@wubzz I suppose you meant schema builder API and column builder API? Looks good though.

Edit: fixed typo meat -> meant 😆

@wubzz
Copy link
Member

wubzz commented Jul 24, 2017

@elhigu Err awkward.. Yes that is what I meant. :)

@alechirsch
Copy link
Contributor

alechirsch commented Aug 14, 2019

Any movement on this?

@kibertoad
Copy link
Collaborator

@alechirsch Noone is actively working on it. PRs are welcome!

@alechirsch
Copy link
Contributor

alechirsch commented Aug 14, 2019

I might look into it if I have time, I just ran into needing this feature. Using raw for now.

@simoami
Copy link

simoami commented Feb 19, 2020

For anyone looking for the raw command:

await knex.raw(`ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${constraint};`)

This is for Postgres and MariaDB. Might need altering for other dbms.

@eyalch
Copy link

eyalch commented Jan 6, 2021

For anyone looking for the raw command:

await knex.raw(`ALTER TABLE ${tableName} DROP CONSTRAINT IF EXISTS ${constraint};`)

This works for MariaDB, but not for MySQL. Haven't tested others.

@cod3cod3
Copy link

any updates on this? can I contribute somehow if it is not yet implemented?

@kibertoad
Copy link
Collaborator

@cod3cod3 yup, PR is welcome!

@louispotok
Copy link

louispotok commented Dec 28, 2023

Took a crack at this, it seems pretty straightforward? If this looks right I can do the same for dropForeign and dropPrimary and add tests as well.

master...louispotok:knex:drop-unique-if-exists

@louispotok
Copy link

@kibertoad Does this look like the right direction?

@louispotok louispotok linked a pull request May 7, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants