2014-10-29 97 views
0

使用提示命令,我已經使用逆向工程生成了一個含有doctine的數據庫。它的工作完美:error1833:無法更改列號

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 

然後,我安裝Symfony的新更新composer.phar

然後,FOSUserBundle爲了管理我的項目中的用戶。

我不得不更新我用這個提示命令,以生成表用戶及實體等主義與數據庫:

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' 

年齡的SQL NCE表:

-- 
-- 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 [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 

這是僱工表的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 [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 

應該有人爲什麼和如何教義解決這一問題?

回答

4

外鍵的問題是,學說有時不能自己解決它們。但它通常會暫時禁用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 

如果這樣的作品沒有引發錯誤,有學說檢查,如果一切都符合的ORM定義:

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

如果這給你更多的SQL查詢來執行,那麼這樣做。之後,你應該沒問題。

+0

轉儲到MySql的導入不起作用,我有這個'錯誤:操作符'<「保留供以後使用' – 2014-10-29 13:58:58

+0

呃,是否有可能在Windows系統上試用這個?如果是這樣,你使用正斜槓是否正確? (我真的不知道,我認爲反斜槓是Windows系統上的目錄分隔符。) – lxg 2014-10-29 17:37:35

+0

無論如何,請嘗試以下方法將轉儲傳遞給MySQL:http://stackoverflow.com/a/2148816/3908235 ('Get-Content ...')。 – lxg 2014-10-29 17:45:01

0

如果你想用這個作爲這裏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();