DoctrineFixturesBundle: doctrine:fixtures:load --purge-with-truncate fails

If I run app/console doctrine:fixtures:load --purge-with-truncate I get the error SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 25 (9 by maintainers)

Most upvoted comments

This seems to be shorter

bin/console doctrine:schema:drop --force \
&& bin/console doctrine:schema:update --force \
&& bin/console doctrine:fixtures:load -n

You can actually execute this command :

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

I created a custom command for doing that , although updating schema every time it is a little bit frustrating.

Creating directory Command inside AppBundle

<?php
namespace AppBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class fixturesReloadCommand extends Command
{
    protected function configure()
    {
        $this
            // the name of the command (the part after "bin/console")
            ->setName('app:fixturesReload')

            // the short description shown while running "php bin/console list"
            ->setDescription('Drop/Create Database and load Fixtures ....')

            // the full command description shown when running the command with
            // the "--help" option
            ->setHelp('This command allows you to load dummy data by recreating database and loading fixtures...');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $application = $this->getApplication();
        $application->setAutoExit(false);

        $output->writeln([
            '===================================================',
            '*********        Dropping DataBase        *********',
            '===================================================',
            '',
        ]);

        $options = array('command' => 'doctrine:database:drop',"--force" => true);
        $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));


        $output->writeln([
            '===================================================',
            '*********        Creating DataBase        *********',
            '===================================================',
            '',
        ]);

        $options = array('command' => 'doctrine:database:create',"--if-not-exists" => true);
        $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));

        $output->writeln([
            '===================================================',
            '*********         Updating Schema         *********',
            '===================================================',
            '',
        ]);

        //Create de Schema
        $options = array('command' => 'doctrine:schema:update',"--force" => true);
        $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));

        $output->writeln([
            '===================================================',
            '*********          Load Fixtures          *********',
            '===================================================',
            '',
        ]);

        //Loading Fixtures
        $options = array('command' => 'doctrine:fixtures:load',"--no-interaction" => true);
        $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));

    }
}

And run the command from terminal

php bin/console app:fixturesReload

Any news on this? why “SET foreign_key_checks = 0;” proposal has been rejected ?

I think SET foreign_key_checks = 0; it’s the only possible way to solve this problem. That’s why it exists. Because if you have a relation with foreign keys between each other, there is no other way.

So this command doesn’t make sense at all to me. 😦

Can we please add something like --ignore-foreign-keys so that I don’t have to edit the core?

Same problem, i run a $ php bin/console doctrine:database:drop --force $ php bin/console doctrine:database:create $ php bin/console doctrine:schema:update --force

to solve (if it is a solution)…

This seems to be doing the job for me as a runtests.sh script:

php bin/console doctrine:database:drop --force --env=test
php bin/console doctrine:database:create --env=test
php bin/console doctrine:schema:update --force --env=test
php bin/console doctrine:fixtures:load --env=test --no-interaction
#test etc
php bin/phpspec run
php bin/behat