[DBAL-377] Rename Table foreignkey not changed Created: 07/Nov/12  Updated: 03/Jan/14  Resolved: 03/Jan/14

Status: Resolved
Project: Doctrine DBAL
Component/s: Schema Managers
Affects Version/s: 2.3
Fix Version/s: None
Security Level: All

Type: Bug Priority: Major
Reporter: Manuel Assignee: Benjamin Eberlei
Resolution: Invalid Votes: 0
Labels: None


 Description   

if i want to change a tablename the foreign key doesnt change and i get an error

$config = new Doctrine\DBAL\Configuration();
$conn = Doctrine\DBAL\DriverManager::getConnection($conn, $config);
$sm = $conn->getSchemaManager();
foreach ($sm->listTables() as $table) {
$fromSchema = $sm->createSchema();
$toSchema = clone $fromSchema;
$toSchema->renameTable($table->getName(), str_replace("_new", "", $table->getName()));
$sql = $fromSchema->getMigrateToSql($toSchema, $conn->getDatabasePlatform());
foreach ($sql as $query)

{ $conn->query($query); }

}

##################################################
CREATE TABLE doc_consultant_practice (consultant_id INT NOT NULL, practice_id INT NOT NULL, INDEX IDX_60C69DE344F779A2 (consultant_id), INDEX IDX_60C69DE3ED33821 (practice_id), PRIMARY KEY(consultant_id, practice_id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB
ALTER TABLE doc_consultant_practice ADD CONSTRAINT FK_60C69DE3ED33821 FOREIGN KEY (practice_id) REFERENCES doc_xml_practice_new (id) ON DELETE CASCADE
ALTER TABLE doc_consultant_practice ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE
DROP TABLE doc_consultant_practice_new
##################################################
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'doc_xml_consultant_new' already exists' in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/ToolsException.php on line 33

PDOException: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'doc_xml_consultant_new' already exists in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/DBAL/Connection.php on line 646

Call Stack:
0.0178 766208 1.

{main}() /PATH/TO/jobs/import.php:0
0.0430 771936 2. require_once('/PATH/TO/jobs/config.php') /PATH/TO/jobs/import.php:7
0.0528 775448 3. require_once('/PATH/TO/jobs/bootstrap.php') /PATH/TO/jobs/config.php:3
0.0623 812680 4. require_once('/PATH/TO/jobs/bootstrap_doctrine.php') /PATH/TO/jobs/bootstrap.php:5
16.7866 16304048 5. Doctrine\ORM\Tools\SchemaTool->createSchema() /PATH/TO/jobs/bootstrap_doctrine.php:70
16.8798 16562624 6. Doctrine\DBAL\Connection->executeQuery() /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/SchemaTool.php:90
16.8804 16564080 7. PDO->query() /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/DBAL/Connection.php:646

Doctrine\DBAL\DBALException: An exception occurred while executing 'CREATE TABLE doc_xml_consultant_new (id INT NOT NULL, name VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, lastCommaFirstName VARCHAR(255) DEFAULT NULL, education LONGTEXT DEFAULT NULL, workHistory LONGTEXT DEFAULT NULL, imageStandard VARCHAR(255) DEFAULT NULL, imageWide VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB':

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'doc_xml_consultant_new' already exists in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/DBAL/DBALException.php on line 47

Call Stack:
0.0178 766208 1. {main}

() /PATH/TO/jobs/import.php:0
0.0430 771936 2. require_once('/PATH/TO/jobs/config.php') /PATH/TO/jobs/import.php:7
0.0528 775448 3. require_once('/PATH/TO/jobs/bootstrap.php') /PATH/TO/jobs/config.php:3
0.0623 812680 4. require_once('/PATH/TO/jobs/bootstrap_doctrine.php') /PATH/TO/jobs/bootstrap.php:5
16.7866 16304048 5. Doctrine\ORM\Tools\SchemaTool->createSchema() /PATH/TO/jobs/bootstrap_doctrine.php:70
16.8798 16562624 6. Doctrine\DBAL\Connection->executeQuery() /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/SchemaTool.php:90

Doctrine\ORM\Tools\ToolsException: Schema-Tool failed with Error 'An exception occurred while executing 'CREATE TABLE doc_xml_consultant_new (id INT NOT NULL, name VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, lastCommaFirstName VARCHAR(255) DEFAULT NULL, education LONGTEXT DEFAULT NULL, workHistory LONGTEXT DEFAULT NULL, imageStandard VARCHAR(255) DEFAULT NULL, imageWide VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB':

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'doc_xml_consultant_new' already exists' while executing DDL: CREATE TABLE doc_xml_consultant_new (id INT NOT NULL, name VARCHAR(255) DEFAULT NULL, email VARCHAR(255) DEFAULT NULL, lastCommaFirstName VARCHAR(255) DEFAULT NULL, education LONGTEXT DEFAULT NULL, workHistory LONGTEXT DEFAULT NULL, imageStandard VARCHAR(255) DEFAULT NULL, imageWide VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB in /Applications/MAMP/bin/php/php5.3.14/lib/php/Doctrine/ORM/Tools/ToolsException.php on line 33

Call Stack:
0.0178 766208 1.

{main}

() /PATH/TO/jobs/import.php:0
0.0430 771936 2. require_once('/PATH/TO/jobs/config.php') /PATH/TO/jobs/import.php:7
0.0528 775448 3. require_once('/PATH/TO/jobs/bootstrap.php') /PATH/TO/jobs/config.php:3
0.0623 812680 4. require_once('/PATH/TO/jobs/bootstrap_doctrine.php') /PATH/TO/jobs/bootstrap.php:5
16.7866 16304048 5. Doctrine\ORM\Tools\SchemaTool->createSchema() /PATH/TO/jobs/bootstrap_doctrine.php:70



 Comments   
Comment by Manuel [ 07/Nov/12 ]

now i find a way to rename the table
foreach ($sm->listTableForeignKeys($table->getName()) as $foreignKey)

{ $sm->dropForeignKey($foreignKey->getName(), $table->getName()); }

and then rename.
but it is right that the rows doesnt copy?
or is there a was that i can do it?

else this would be a good feature

Comment by Steve Müller [ 28/Dec/13 ]

Manuel You are using the schema manager in a wrong way. What you should do to rename a table through the schema manager directly:

//...

foreach ($sm->listTables() as $table) {
    $sm->renameTable($table->getName, str_replace("_new", "", $table->getName()));
}

... and you are done.

Comment by Steve Müller [ 28/Dec/13 ]

Benjamin Eberlei close, please (I cannot do that because of this stupid "Awaiting Feedback" status).

Generated at Fri Apr 25 01:11:17 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.