question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support "ADD COLUMN IF NOT EXISTS"

See original GitHub issue

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don’t fill in the requested information
  • I have read the contribution guidelines

Feature Description

Describe the feature you’d like to see implemented

When creating a new column with addColumn, I’d like to be able to provide an option for the method to not crash if the column already exists. For example:

await queryInterface.addColumn('MyTable', 'new_col', {
  type: Sequelize.INTEGER,
}, {
  mustExist: false, // Prevents crashing if the column already exists
);

Using Postgres as a reference, the generated SQL would look like this:

ALTER TABLE "MyTable" ADD COLUMN IF NOT EXISTS "new_col";

Describe why you would like this feature to be added to Sequelize

Because migrations don’t happen in transactions, you can end up with a partially-executed migration that needs to be re-triggered. Adding support for this option would make it easier to have resiliant migrations that can be executed multiple times.

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s):

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don’t have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don’t have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.

Context

Strongly related to https://github.com/sequelize/sequelize/issues/11774


Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as “+1” will be removed.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ephyscommented, Oct 10, 2022

Feel free 😃 My only note is, because of the support table (does any other dialect than postgres support this?) I think this shouldn’t be the default & add a ifNotExists option. If the dialect doesn’t support IF NOT EXISTS, it should throw instead of silently ignoring the option

2reactions
ephyscommented, Sep 17, 2022

Sounds like a nice addition, we welcome it. I think dialects that don’t support it and don’t have a clean workaround could query whether the column exists first

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL: How to create Column if not exists?
Find if the column exists using the SQL below: SELECT column_name FROM INFORMATION_SCHEMA . COLUMNS WHERE TABLE_SCHEMA =[Database Name] AND TABLE_NAME =[Table ...
Read more >
MySQL: ALTER TABLE if column not exists - Stack Overflow
Use the following in a stored procedure: IF NOT EXISTS( SELECT NULL FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tablename' AND table_schema ...
Read more >
MS SQL Server - How to check and add a column if it doesn't ...
IF NOT EXISTS ( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name' AND COLUMN_NAME = 'col_name') BEGIN ALTER TABLE ...
Read more >
Support IF NOT EXISTS in alter table add columns - Apache
Turns out our existing syntax is: ALTER TABLE <table> (REPLACE|ADD) COLUMNS ( ... ) That is, our syntax allows multiple columns per statement, ......
Read more >
ALTER TABLE - Vertica
table. The table to alter. ADD COLUMN. Adds a column to the table and, by default, to all its superprojections: ADD COLUMN [IF...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found