Use the view table_constraints in the information_schema schema. The column table_name gives you the name of the table in which the constraint is defined, and the column constraint_name contains the name of the constraint.
Try this:
Remove the constraint DF_Movies_Rating__48CFD27E before changing your field type.
The constraint is typically created automatically by the DBMS (SQL Server).
To see the constraint associated with the table, expand the table attributes in Object explorer, followed by the category Constraints as shown below:
You must remove the constraint before changing the field type.
I'm adding this as a response to explain where the constraint comes from. I tried to do it in the comments but it's hard to edit nicely there :-/
If you create (or alter) a table with a column that has default values it will create the constraint for you.
In your table for example it might be:
CREATE TABLE Movie (
...
rating INT NOT NULL default 100
)
It will create the constraint for default 100.
If you instead create it like so
CREATE TABLE Movie (
name VARCHAR(255) NOT NULL,
rating INT NOT NULL CONSTRAINT rating_default DEFAULT 100
);
Then you get a nicely named constraint that's easier to reference when you are altering said table.
ALTER TABLE Movie DROP CONSTRAINT rating_default;
ALTER TABLE Movie ALTER COLUMN rating DECIMAL(2) NOT NULL;
-- sets up a new default constraint with easy to remember name
ALTER TABLE Movie ADD CONSTRAINT rating_default DEFAULT ((1.0)) FOR rating;
You can combine those last 2 statements so you alter the column and name the constraint in one line (you have to if it's an existing table anyways)
This is the tsql
way
ALTER TABLE yourtable DROP CONSTRAINT constraint_name -- DF_Movies_Rating__48CFD27E
For completeness, this just shows @Joe Taras's comment as an answer
As constraint has unpredictable name, you can write special script(DropConstraint) to remove it without knowing it's name (was tested at EF 6.1.3):
public override void Up()
{
DropConstraint();
AlterColumn("dbo.MyTable", "Rating", c => c.Double(nullable: false));
}
private void DropConstraint()
{
Sql(@"DECLARE @var0 nvarchar(128)
SELECT @var0 = name
FROM sys.default_constraints
WHERE parent_object_id = object_id(N'dbo.MyTable')
AND col_name(parent_object_id, parent_column_id) = 'Rating';
IF @var0 IS NOT NULL
EXECUTE('ALTER TABLE [dbo].[MyTable] DROP CONSTRAINT [' + @var0 + ']')");
}
public override void Down()
{
AlterColumn("dbo.MyTable", "Rating", c => c.Int(nullable: false));
}
MS SQL Studio take care of when you delete the column but if you need to Delete Constraint Programmatically here is simple solution
Here’s a code snippet that’ll drop a column with a default constraint:
DECLARE @ConstraintName nvarchar(200)
SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('__TableName__') AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = N'__ColumnName__' AND object_id = OBJECT_ID(N'__TableName__'))
IF @ConstraintName IS NOT NULL
EXEC('ALTER TABLE __TableName__ DROP CONSTRAINT ' + @ConstraintName)
IF EXISTS (SELECT * FROM syscolumns WHERE id=object_id('__TableName__') AND name='__ColumnName__')
EXEC('ALTER TABLE __TableName__ DROP COLUMN __ColumnName__')
Just replace TableName and ColumnName with the appropriate values. You can safely run this even if the column has already been dropped.
Bonus: Here’s the code to drop foreign keys and other types of constraints.
IF EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '__TableName__' AND COLUMN_NAME = '__ColumnName__')
BEGIN
SELECT @ConstraintName = CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE where TABLE_NAME = '__TableName__' AND COLUMN_NAME = '__ColumnName__'
EXEC('ALTER TABLE __TableName__ DROP CONSTRAINT ' + @ConstraintName)
END
Blog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With