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

MSSQL: Add option to add a name for implicit default constraints #193

Open
Arnagos opened this issue Aug 21, 2024 · 1 comment
Open

MSSQL: Add option to add a name for implicit default constraints #193

Arnagos opened this issue Aug 21, 2024 · 1 comment

Comments

@Arnagos
Copy link

Arnagos commented Aug 21, 2024

Currently, the SQL generated for implicit default constraints is something like this:
ALTER TABLE TableName ADD NewColumn Type NOT NULL DEFAULT (Value)

This creates an anonym default constraint in MSSQL, which makes the management of the constraint very unpleasant and forces you to reload the schema from the DB to get the generated name.

Instead, I'd like to just set the name it uses for the constraint like this:
ALTER TABLE TableName ADD NewColumn Type NOT NULL CONSTRAINT ConstraintName DEFAULT (Value). This way it's possible to add the constraint to the DatabaseTable instance as well. Currently, it's not possible to add a new column with default x and then replace the default value with y for future inserts without reloading the schema.

Note: I'm using IMigrationGenerator.AddColumn() but AddTable() would also be nice.

Another note: It's not really necessary that I can define the name of the constraint. It just needs a name and has to be added to the table so that further operations with the default are possible. So a default DF_TableName_ColumnName is perfectly fine.

Normally I would just write my own method that just minimally changes your code but all the utils, etc. are marked as internal, so I can't use them.

martinjw added a commit that referenced this issue Oct 10, 2024
@martinjw
Copy link
Owner

I just ran into the problem in Oracle. You'll need to define a check constraint which has the expression 'NewColumn IS NOT NULL', which is what the automatic constraint will look like anyway. The AddColumn migration checks the CheckConstraints for this pattern when it's not nullable- when it's a match, he constraint is named in the ALTER TABLE x ADD y

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants