Skip to content

Adding support for changing column default value - #3442

Closed
fduch2k wants to merge 1 commit into
sequelize:masterfrom
fduch2k:feature/mssql-default-constraint-support
Closed

fduch2k wants to merge 1 commit into
sequelize:masterfrom
fduch2k:feature/mssql-default-constraint-support

Conversation

@fduch2k

@fduch2k fduch2k commented Apr 1, 2015

Copy link
Copy Markdown

Current version of mssql query-generator generate incorrect sql code for changing column default value.
E.g. migration.changeColumn('Users', 'flags', {type: DataTypes.INTEGER, defaultValue: 1}); generate next code:

ALTER TABLE [Users] ALTER COLUMN [flags] INTEGER DEFAULT 1;

but this code is generate next error:

Incorrect syntax near the keyword 'DEFAULT'

After some researching I found that in tsql default values implemented over constraints, so changing value should be s.th. like this:

ALTER TABLE [Users] ALTER COLUMN [flags] INTEGER; 
IF OBJECT_ID('df_users_flags', 'D') IS NOT NULL ALTER TABLE [Users] DROP CONSTRAINT df_users_flags; 
ALTER TABLE [Users] ADD CONSTRAINT df_users_flags DEFAULT 1 FOR [flags]

This patch implements described behavior by adding constraint with name df_tablename_fieldname for default values.

Useful links:
http://stackoverflow.com/questions/2095289/sql-server-alter-column-adding-default-constraint
http://stackoverflow.com/questions/1782714/declaring-a-default-constraint-when-creating-a-table
http://stackoverflow.com/questions/92082/add-a-column-with-a-default-value-to-an-existing-table-in-sql-server

@mickhansen mickhansen added the dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects). label Apr 1, 2015
@janmeier

Copy link
Copy Markdown
Member

Sorry for neglecting this PR for so long @fduch2k !!

The code looks good, can you add some tests as well? Both integration test (calling the function) and unit tests (testing that the generated sql is correct) would be nice

@fduch2k

fduch2k commented Jun 25, 2015

Copy link
Copy Markdown
Author

Sure, I'll add tests

@janmeier

Copy link
Copy Markdown
Member

Sweet, and thanks for the quick reply! (which is more than you can say about us...)

Remember to rebase so your tests can be run on travis

@bassarisse

Copy link
Copy Markdown

Hi!

My team is a bit anal about all constraint names (fk, pk, etc) in mssql, for reasons, so I customized sequelize a bit to generate them with a default name (similar to what was done in this PR) and also to offer the ability to specify the names by yourself. You can check it here:
https://github.com/bassarisse/sequelize/commits/master

Maybe we can join these modifications?

My idea is receiving the custom constraint names in the options argument on .changeColumn() and also on .removeColumn() (maybe .renameColumn() too?), and if not specified, the default constraint name would be used.

@janmeier

Copy link
Copy Markdown
Member

@bassarisse That sounds like a good addition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dialect: mssql For issues and PRs. Things that involve MSSQL (and do not involve all dialects).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants