doctrine / DoctrineFixturesBundle

Symfony integration for the doctrine/data-fixtures library
MIT License
2.44k stars 202 forks source link

Purge with truncate not working when entities have one-to-many relations due to FK constraints #390

Open crtl opened 1 year ago

crtl commented 1 year ago

When trying to run doctrine:fixtures:load --purge-with-truncate the command will fail and display "There is no active Transaction" error. Seems to be related to #348 #363 but its not (to my knowledge). Its caused by foreign key constraints on the table because when trying to truncate one of my tables mysql server will complain that:

> TRUNCATE target_table;

#1701 - Cannot truncate a table referenced in a foreign key constraint (`database_name`.`target_table`, CONSTRAINT `FK_571EF6874B89032C` FOREIGN KEY (`referencing_column`) REFERENCES `database_name`.`referenced_table` (`id`))

Possible solution would be to disable FOREIG_KEY_CHECKS before purging and enabling them afterwards.

Exception:

user@6de99abff372:/var/www/html$ php bin/console doc:fix:load --env test --purge-with-truncate -vvv

 Careful, database "database_name" will be purged. Do you want to continue? (yes/no) [no]:
 > y

   > purging database

In PDOException.php line 20:

  [Doctrine\DBAL\Driver\PDO\PDOException]  
  There is no active transaction           

Exception trace:
  at /var/www/html/vendor/doctrine/dbal/src/Driver/PDO/PDOException.php:20
 Doctrine\DBAL\Driver\PDO\PDOException::new() at /var/www/html/vendor/doctrine/dbal/src/Driver/PDO/Connection.php:132
 Doctrine\DBAL\Driver\PDO\Connection->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Driver/Middleware/AbstractConnectionMiddleware.php:86
 Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Logging/Connection.php:80
 Doctrine\DBAL\Logging\Connection->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Driver/Middleware/AbstractConnectionMiddleware.php:86
 Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware->rollBack() at /var/www/html/vendor/symfony/doctrine-bridge/Middleware/Debug/Connection.php:173
 Symfony\Bridge\Doctrine\Middleware\Debug\Connection->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Connection.php:1514
 Doctrine\DBAL\Connection->rollBack() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:280
 Doctrine\ORM\EntityManager->wrapInTransaction() at /var/www/html/var/cache/test/ContainerXojsjWE/EntityManager_9a5be93.php:73
 ContainerXojsjWE\EntityManager_9a5be93->wrapInTransaction() at /var/www/html/vendor/doctrine/data-fixtures/src/Executor/ORMExecutor.php:28
 Doctrine\Common\DataFixtures\Executor\ORMExecutor->execute() at /var/www/html/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php:159
 Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1058
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:301
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:82
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /var/www/html/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /var/www/html/vendor/autoload_runtime.php:35
 require_once() at /var/www/html/bin/console:11

In Connection.php line 130:

  [PDOException]                  
  There is no active transaction  

Exception trace:
  at /var/www/html/vendor/doctrine/dbal/src/Driver/PDO/Connection.php:130
 PDO->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Driver/PDO/Connection.php:130
 Doctrine\DBAL\Driver\PDO\Connection->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Driver/Middleware/AbstractConnectionMiddleware.php:86
 Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Logging/Connection.php:80
 Doctrine\DBAL\Logging\Connection->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Driver/Middleware/AbstractConnectionMiddleware.php:86
 Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware->rollBack() at /var/www/html/vendor/symfony/doctrine-bridge/Middleware/Debug/Connection.php:173
 Symfony\Bridge\Doctrine\Middleware\Debug\Connection->rollBack() at /var/www/html/vendor/doctrine/dbal/src/Connection.php:1514
 Doctrine\DBAL\Connection->rollBack() at /var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:280
 Doctrine\ORM\EntityManager->wrapInTransaction() at /var/www/html/var/cache/test/ContainerXojsjWE/EntityManager_9a5be93.php:73
 ContainerXojsjWE\EntityManager_9a5be93->wrapInTransaction() at /var/www/html/vendor/doctrine/data-fixtures/src/Executor/ORMExecutor.php:28
 Doctrine\Common\DataFixtures\Executor\ORMExecutor->execute() at /var/www/html/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php:159
 Doctrine\Bundle\FixturesBundle\Command\LoadDataFixturesDoctrineCommand->execute() at /var/www/html/vendor/symfony/console/Command/Command.php:298
 Symfony\Component\Console\Command\Command->run() at /var/www/html/vendor/symfony/console/Application.php:1058
 Symfony\Component\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /var/www/html/vendor/symfony/console/Application.php:301
 Symfony\Component\Console\Application->doRun() at /var/www/html/vendor/symfony/framework-bundle/Console/Application.php:82
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /var/www/html/vendor/symfony/console/Application.php:171
 Symfony\Component\Console\Application->run() at /var/www/html/vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:54
 Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner->run() at /var/www/html/vendor/autoload_runtime.php:35
 require_once() at /var/www/html/bin/console:11

doctrine:fixtures:load [--append] [--group GROUP] [--em EM] [--purger PURGER] [--purge-exclusions PURGE-EXCLUSIONS] [--shard SHARD] [--purge-with-truncate]

Environment: MariaDB: Server-Version: 10.4.13-MariaDB-1:10.4.13+maria~bionic - mariadb.org binary distribution

php -v
PHP 8.1.16 (cli) (built: Mar  1 2023 12:41:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.16, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.16, Copyright (c), by Zend Technologies
    with Xdebug v3.2.0, Copyright (c) 2002-2022, by Derick Rethans

user@6de99abff372:/var/www/html$ composer show -i doctrine/*
You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.
doctrine/annotations                1.14.3 Docblock Annotations Parser
doctrine/cache                      2.2.0  PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.
doctrine/collections                2.1.2  PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common                     3.4.3  PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, proxies and much more.
doctrine/data-fixtures              1.6.6  Data Fixtures for all Doctrine Object Managers
doctrine/dbal                       3.6.2  Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/deprecations               v1.0.0 A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.
doctrine/doctrine-bundle            2.9.1  Symfony DoctrineBundle
doctrine/doctrine-fixtures-bundle   3.4.4  Symfony DoctrineFixturesBundle
doctrine/doctrine-migrations-bundle 3.2.2  Symfony DoctrineMigrationsBundle
doctrine/event-manager              1.2.0  The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector                  2.0.6  PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.
doctrine/instantiator               2.0.0  A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                      2.1.0  PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                 3.5.5  PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and ...
doctrine/orm                        2.15.1 Object-Relational-Mapper for PHP
doctrine/persistence                3.1.4  The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/sql-formatter              1.1.3  a PHP SQL highlighting library