DBAL-255: On SQL SERVER Trying to drop a column throws error because of auto generated Constraints #1432

Closed
doctrinebot opened this Issue Apr 13, 2012 · 4 comments

2 participants

@doctrinebot

Jira issue originally created by user fredcallagan:

Whenever Doctrine tries to drop a column that has some implicit constraints the system the SQL Native Client throws the message:

The Object 'NameOf_The_Object' is dependent on column 'Column_ToDrop';

This is probably because columns such as Decimal Numbers have an automatice generated constraint that need to be dropped in order to drop a column.

A possible Solution is to add a platform specific getConstratintForTableSQL that queries the database to get all the constraint for the given column, than override the 'alterTable' method in the SQLServer Schema Manager to first drop the constraint than the columns in question by going and checking all the Columns that needs to be dropped in the TableDiff passed.

Something similar to this but better refactored I guess:

Changes in SQLServerPlatform.php

Add a method similar to this:

    /****
     * This function retrieves the constraints for a given column that is going to be droppped 
     */
    public function getColumnConstraintSQL($table, $column)
    {
        return "SELECT SysObjects.[Name]
                From SysObjects Inner Join (Select [Name],[ID] From SysObjects Where XType = 'U') As Tab
                On Tab.[ID] = Sysobjects.[Parent_Obj] 
                Inner Join sysconstraints On sysconstraints.Constid = Sysobjects.[ID] 
                Inner Join SysColumns Col On Col.[ColID] = sysconstraints.[ColID] And Col.[ID] = Tab.[ID]
                Where Col.[Name] = '$column' and Tab.[Name] = '$table'
                order by Col.[Name]";
    }

Changes on SqlServerChemaManager.php

       /****
    * Override
    */
    public function alterTable(TableDiff $tableDiff)
    {
        if(count($tableDiff->removedColumns) > 0){
            $constraintsSql = array();
            foreach($tableDiff->removedColumns as $col){
                $constraintsSql[] = $this->_platform->getColumnConstraintSQL($tableDiff->name, $col->oldColumnName);
            }
            $constraintsToDrop = array();
            foreach($constraintsSql as $sql){
                 $constraintsToDrop[] = $this->_conn->execute($sql);
            }
            foreach($constraintsToDrop as $constraint){
                $this->_conn->execute("ALTER TABLE $tableDiff->name DROP CONSTRAINT $constraint");
            }
        }
        return parent::alterTable($tableDiff);
    }

Hope this helps

Thx

@doctrinebot

Comment created by fredcallagan:

This version of the getColumnConstraintSQL seems better:

       /****
    * Override
    */
    public function alterTable(TableDiff $tableDiff)
    {
        if(isset($tableDiff->removedColumns) && count($tableDiff->removedColumns) > 0){
            $constraintsSql = array();
            foreach($tableDiff->removedColumns as $col){
                $constraintsSql[] = $this->_platform->getColumnConstraintSQL($tableDiff->name, $col->getName());
            }
            $constraintsToDrop = array();
            foreach($constraintsSql as $sql){
                 $constraintData = $this->_conn->fetchAll($sql);
                 foreach($constraintData as $keyCostr => $costraint){
                    $constraintsToDrop[] = $costraint['Name'];
                 }
            }

            foreach($constraintsToDrop as $key => $constraint){
                $this->_conn->exec("ALTER TABLE $tableDiff->name DROP CONSTRAINT ".$constraint);
            }
        }
        return parent::alterTable($tableDiff);
    }
@doctrinebot

Comment created by @beberlei:

Fixed formatting

@doctrinebot

Comment created by @beberlei:

Fixed.

I had to adjust your SQL a little since "sysconstraints" is deprecated and not exists anymore on SQL Azure for example.

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot doctrinebot added the Bug label Dec 6, 2015
@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment