Support "ADD COLUMN IF NOT EXISTS"
See original GitHub issueIssue 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):
- Postgres:
ADD COLUMN IF NOT EXISTS
is officially supported - MySQL: Not officially supported. Possible workarounds?
- MSSQL: Not officially supported. Possible workarounds?
- SQLite: Not officially supported. Possible workarounds?
- Postgres:
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:
- Created a year ago
- Reactions:2
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 supportIF NOT EXISTS
, it should throw instead of silently ignoring the optionSounds 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