【问题标题】:error1833: cannot change colum id错误 1833:无法更改列 ID
【发布时间】:2014-10-29 10:16:29
【问题描述】:

通过提示命令,我使用 逆向工程 生成了一个带有 doctine 的数据库。效果很好:

php app/console doctrine:mapping:convert yml ./src/Gir/DatabaseBundle/Resources/config/doctrine/metadata/orm --from-database --force

之后,我创建了实体

php app/console doctrine:mapping:import GirDatabaseBundle annotation

还有注释

php app/console doctrine:generate:entities GirDatabaseBundle

然后,我使用 composer.phar 安装了 Symfony 的新更新。

然后,FOSUserBundle 来管理我项目中的用户。

为了生成表用户和实体等,我必须使用 doctrine更新我的数据库:

php app/console doctrine:schema:update --force

当我执行此命令时,学说在 windows 提示命令中向我发送此 错误

**[Doctrine\DBAL\DBALException]**
An exception occured while executing 'ALTER TABLE agence CHANGE id id INT NOT NULL':
SQLSTATE[HY000]: General error: 1833 Cannot change colum 'id': used in a foreign key constraint 'fk_employe_agence1' of table 'gir.employe'

**[PDOException]**
SQLSTATE[HY000]: General error: 1833 Cannot change colum 'id': used in a foreign key constraint 'fk_employe_agence1' of table 'gir.employe'

这是agence表的SQL:

--
-- Base de données :  `gir`
--

-- --------------------------------------------------------

--
-- Structure de la table `agence`
--

CREATE TABLE IF NOT EXISTS `agence` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nom` varchar(150) NOT NULL,
  `nomVoie` varchar(150) NOT NULL,
  `numVoie` int(5) DEFAULT NULL,
  `codePostal` int(5) NOT NULL,
  `comune` varchar(150) NOT NULL,
  `telephone` varchar(150) NOT NULL,
  `fax` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  `entreprises_id` int(11) NOT NULL,
  PRIMARY KEY (`id`,`entreprises_id`),
  KEY `fk_agence_entreprises1_idx` (`entreprises_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

这是employe表的SQL:

--
-- Base de données :  `enexgir`
--

-- --------------------------------------------------------

--
-- Structure de la table `employe`
--

CREATE TABLE IF NOT EXISTS `employe` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nom` varchar(45) NOT NULL,
  `prenom` varchar(45) NOT NULL,
  `poste` varchar(45) NOT NULL,
  `portable` varchar(45) NOT NULL,
  `ligneDirecte` varchar(45) NOT NULL,
  `faxDirect` varchar(45) NOT NULL,
  `email` varchar(150) NOT NULL,
  `etat` tinyint(1) NOT NULL,
  `agence_id` int(11) NOT NULL,
  PRIMARY KEY (`id`,`agence_id`),
  KEY `fk_employe_agence1_idx` (`agence_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- Contraintes pour les tables exportées
--

--
-- Contraintes pour la table `employe`
--
ALTER TABLE `employe`
  ADD CONSTRAINT `fk_employe_agence1` FOREIGN KEY (`agence_id`) REFERENCES `agence` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

有人应该为什么以及如何用教义来解决这个问题?

【问题讨论】:

    标签: mysql symfony pdo doctrine-orm alter-table


    【解决方案1】:

    外键的问题是 Doctrine 有时不能自己解决它们。但它通常对您暂时禁用 FK 有效。请尝试以下操作:

    准备一个 SQL 转储,它将在导入期间禁用 FK 检查。然后告诉 Doctrine 将更改转储到文件中,而不是直接应用它们。然后用 MySQL 导入文件。

    # disable FK checks during the import
    echo 'SET FOREIGN_KEY_CHECKS = 0;' > dump.sql
    
    # append the DB dump
    app/console doctrine:schema:update --dump-sql --complete >> dump.sql
    
    # import the dump into MySQL
    mysql -u username -p -D dbname < dump.sql
    

    如果这样做不会引发错误,请让 Doctrine 检查一切是否与 ORM 定义一致:

    app/console doctrine:schema:update --dump-sql --complete
    

    如果这为您提供了更多要执行的 SQL 查询,请执行此操作。之后,你应该会没事的。

    【讨论】:

    • 将dump导入MySql不起作用,我有这个error:the operator "&lt;" is reserved for a later use
    • 呃,你有没有可能在 Windows 系统上尝试这个?如果是这样,您使用正斜杠是否正确? (我真的不知道,我以为反斜杠是Windows系统上的目录分隔符。)
    • 无论如何,请尝试以下方法将转储传递给 MySQL:stackoverflow.com/a/2148816/3908235 (Get-Content …)。
    • 是的,我正在使用 Windows 系统。我试着喜欢你的链接,我会告诉你它是否有效。谢谢。
    【解决方案2】:

    这个解决方案是正确的:https://stackoverflow.com/a/26628581/2400373

    但是,如果您使用 Symfony 3,则正确的是:

    bin/console doctrine:schema:update --dump-sql --complete >> dump.sql
    

    在此指令之前从未使用过指令php

    【讨论】:

      【解决方案3】:

      如果您想将其用作 DataFixture,我在 load 方法中是这样做的:

          $dumpFilePath  = "dump.sql";
          file_put_contents($dumpFilePath, "SET FOREIGN_KEY_CHECKS = 0; \n");
      
          $kernel = $this->container->get('kernel');
          $application = new Application($kernel);
          $application->setAutoExit(false);
          $options = array('command' => 'doctrine:schema:update', '--dump-sql' => true, '--complete' => true);
      
          $input = new ArrayInput($options);
          $output = new StreamOutput(fopen($dumpFilePath, 'a'));
          $application->run($input, $output);
      
          $entityManager->getConnection()->prepare(file_get_contents($dumpFilePath))->execute();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-16
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        相关资源
        最近更新 更多