Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALTER TABLE DROP COLUMN failed because one or more objects access this column

I am trying to do this:

ALTER TABLE CompanyTransactions DROP COLUMN Created 

But I get this:

Msg 5074, Level 16, State 1, Line 2 The object 'DF__CompanyTr__Creat__0CDAE408' is dependent on column 'Created'. Msg 4922, Level 16, State 9, Line 2 ALTER TABLE DROP COLUMN Created failed because one or more objects access this column.

This is a code first table. Somehow the migrations have become all messed up and I am trying to manually roll back some changed.

I have no idea what this is:

DF__CompanyTr__Creat__0CDAE408 
like image 961
Casey Crookston Avatar asked Apr 21 '17 18:04

Casey Crookston


People also ask

Can we drop column using alter?

Syntax. The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.

What is ALTER TABLE drop column?

The DROP COLUMN command is used to delete a column in an existing table.

How do I drop a column in SQL?

Right-click the column you want to delete and choose Delete Column from the shortcut menu. If the column participates in a relationship (FOREIGN KEY or PRIMARY KEY), a message prompts you to confirm the deletion of the selected columns and their relationships. Choose Yes.

What does the ALTER TABLE clause with drop will do?

Use the DROP CONSTRAINT clause to destroy an existing constraint whose name you specify. This syntax fragment is part of the ALTER TABLE statement.


1 Answers

You must remove the constraints from the column before removing the column. The name you are referencing is a default constraint.

e.g.

alter table CompanyTransactions drop constraint [df__CompanyTr__Creat__0cdae408]; alter table CompanyTransactions drop column [Created]; 
like image 166
SqlZim Avatar answered Sep 18 '22 15:09

SqlZim