{"id":1620,"date":"2022-08-30T15:18:03","date_gmt":"2022-08-30T15:18:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/doctrine-backuptables-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:18:03","modified_gmt":"2022-08-30T15:18:03","slug":"doctrine-backuptables-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/doctrine-backuptables-collection-of-common-programming-errors\/","title":{"rendered":"Doctrine Backuptables-Collection of common programming errors"},"content":{"rendered":"<p>is there any way to make Backup Tables in Doctrine 2.<\/p>\n<p>I have an XML and want to import it every day with an cron job. First I create a new Schema with the end _new.<\/p>\n<pre><code>if ($newTables) {\n    $tablePastfix = new \\DoctrineExtensions\\TablePastfix('_new');\n    $evm-&gt;addEventListener(\\Doctrine\\ORM\\Events::loadClassMetadata, $tablePastfix);\n    }\n\n    $entityManager = \\Doctrine\\ORM\\EntityManager::create($conn, $config, $evm);\n\n    if ($newTables) {\n    $tool = new \\Doctrine\\ORM\\Tools\\SchemaTool($entityManager);\n    $classes = array (\n        $entityManager-&gt;getClassMetadata('MyClasses'),\n        $entityManager-&gt;getClassMetadata('MyClasses'),\n        $entityManager-&gt;getClassMetadata('MyClasses'),\n    );\n        $tool-&gt;createSchema($classes);\n}\n<\/code><\/pre>\n<p>After it i import the data from xml. And is all okay, i rename the Tables<\/p>\n<pre><code>$dm = Doctrine\\DBAL\\DriverManager::getConnection($conn);\n$sql = \"DROP TABLE `mytable_old`;\";\n$stmt = $dm-&gt;query($sql);\n$stmt-&gt;execute();\n\n$dm = Doctrine\\DBAL\\DriverManager::getConnection($conn);\n$sql = \"ALTER TABLE `mytable` RENAME `mytable_old`;\";\n$stmt = $dm-&gt;query($sql);\n$stmt-&gt;execute();\n\n$dm = Doctrine\\DBAL\\DriverManager::getConnection($conn);\n$sql = \"ALTER TABLE `mytable_new` RENAME `mytable`;\";\n$stmt = $dm-&gt;query($sql);\n$stmt-&gt;execute();\n<\/code><\/pre>\n<p>But when i want to import again, i get the following error . . . . . \ud83d\ude41<\/p>\n<pre><code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121)' in \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/ORM\/Tools\/ToolsException.php on line 33\n\nPDOException: SQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121) in \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/DBAL\/Connection.php on line 646\n\nCall Stack:\n    0.0011     761296   1. {main}() \/path\/to\/jobs\/import.php:0\n    0.0014     767024   2. require_once('\/path\/to\/jobs\/config.php') \/path\/to\/jobs\/import.php:7\n    0.0017     770536   3. require_once('\/path\/to\/jobs\/bootstrap.php') \/path\/to\/jobs\/config.php:3\n    0.0020     796264   4. require_once('\/path\/to\/jobs\/bootstrap_doctrine.php') \/path\/to\/jobs\/bootstrap.php:5\n    0.1468    7673040   5. Doctrine\\ORM\\Tools\\SchemaTool-&gt;createSchema() \/path\/to\/jobs\/bootstrap_doctrine.php:53\n    1.1216    9445080   6. Doctrine\\DBAL\\Connection-&gt;executeQuery() \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/ORM\/Tools\/SchemaTool.php:90\n    1.1216    9445656   7. PDO-&gt;query() \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/DBAL\/Connection.php:646\n\nDoctrine\\DBAL\\DBALException: An exception occurred while executing 'ALTER TABLE doc_consultant_practice_new ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE':\n\nSQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121) in \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/DBAL\/DBALException.php on line 47\n\nCall Stack:\n    0.0011     761296   1. {main}() \/path\/to\/jobs\/import.php:0\n    0.0014     767024   2. require_once('\/path\/to\/jobs\/config.php') \/path\/to\/jobs\/import.php:7\n    0.0017     770536   3. require_once('\/path\/to\/jobs\/bootstrap.php') \/path\/to\/jobs\/config.php:3\n    0.0020     796264   4. require_once('\/path\/to\/jobs\/bootstrap_doctrine.php') \/path\/to\/jobs\/bootstrap.php:5\n    0.1468    7673040   5. Doctrine\\ORM\\Tools\\SchemaTool-&gt;createSchema() \/path\/to\/jobs\/bootstrap_doctrine.php:53\n    1.1216    9445080   6. Doctrine\\DBAL\\Connection-&gt;executeQuery() \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/ORM\/Tools\/SchemaTool.php:90\n\nDoctrine\\ORM\\Tools\\ToolsException: Schema-Tool failed with Error 'An exception occurred while executing 'ALTER TABLE doc_consultant_practice_new ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE':\n\nSQLSTATE[HY000]: General error: 1005 Can't create table 'databasename.#sql-220_87' (errno: 121)' while executing DDL: ALTER TABLE doc_consultant_practice_new ADD CONSTRAINT FK_60C69DE344F779A2 FOREIGN KEY (consultant_id) REFERENCES doc_xml_consultant_new (id) ON DELETE CASCADE in \/Applications\/MAMP\/bin\/php\/php5.3.14\/lib\/php\/Doctrine\/ORM\/Tools\/ToolsException.php on line 33\n\nCall Stack:\n    0.0011     761296   1. {main}() \/path\/to\/jobs\/import.php:0\n    0.0014     767024   2. require_once('\/path\/to\/jobs\/config.php') \/path\/to\/jobs\/import.php:7\n    0.0017     770536   3. require_once('\/path\/to\/jobs\/bootstrap.php') \/path\/to\/jobs\/config.php:3\n    0.0020     796264   4. require_once('\/path\/to\/jobs\/bootstrap_doctrine.php') \/path\/to\/jobs\/bootstrap.php:5\n    0.1468    7673040   5. Doctrine\\ORM\\Tools\\SchemaTool-&gt;createSchema() \/path\/to\/jobs\/bootstrap_doctrine.php:53\n<\/code><\/pre>\n<p>Do anybody know my mistake or have an better idea to realize backup tables?<\/p>\n<ol>\n<li>\n<p>it looks like you have an issue because of <strong>index<\/strong> name duplication, prefixing tables does not prefix indexes for them. You should use a different database for that kind of functionality, you can rename it afterwards or sync data with two connections opened.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:08:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>is there any way to make Backup Tables in Doctrine 2. I have an XML and want to import it every day with an cron job. First I create a new Schema with the end _new. if ($newTables) { $tablePastfix = new \\DoctrineExtensions\\TablePastfix(&#8216;_new&#8217;); $evm-&gt;addEventListener(\\Doctrine\\ORM\\Events::loadClassMetadata, $tablePastfix); } $entityManager = \\Doctrine\\ORM\\EntityManager::create($conn, $config, $evm); if ($newTables) { $tool [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1620","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1620"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1620\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}