2

I've been struggling for several hours on my Symfony2 project fixtures. Here is the error I get :

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`wpanel_dev`.`sshuser`, CONSTRAINT `FK_612DE3EE18F45C82` FOREIGN KEY (`website_id`) REFERENCES `Website` (`id`))  

Having read a lot of answers, I tried this method without success :

php app/console doctrine:database:drop --force
php app/console doctrine:database:create
php app/console doctrine:schema:update --force
php app/console doctrine:fixtures:load (with or without --append)

Here is the fixture code that is generating the error ($new->setWebsite($this->getReference('website-'.$i));)

<?php
namespace WPanel\AdminBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use WPanel\AdminBundle\Entity\SSHUser;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class LoadSSHUserData extends AbstractFixture implements OrderedFixtureInterface
{   
    public function getOrder()
    {
        return 5;
    }

    public function load(ObjectManager $manager)
    {

        for($i = 1; $i <= 20; $i++) {
            $new = new SSHUser();
            $new->setUser('sshuser'.$i);
            $new->setPassword('sshuser'.$i);
            $new->setPath('/var/www/website'.$i.'.com');
            $new->setWebsite($this->getReference('website-'.$i));

            $manager->persist($new);
        }

        $manager->flush();
    }
}
?>

I'm sure the reference is ok. I've been using add/getReference on other classes without any problem.

Thanks for your help !

2 Answers2

0

This error means that there isn't any record on the website table with that ID if I understood correctly.

Either there is a problem with your ordering, or typo in reference.

Hakan Deryal
  • 2,843
  • 1
  • 20
  • 19
0

It seems that you do not have 20 websites object fixtures since you are using the $i counter from 0 to 20.

sf_tristanb
  • 8,725
  • 17
  • 74
  • 118