Skip to content

Duplicate primary key (uuid) #352

Description

@grztofdev

My fixture file looks like:

<?php
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Faker\Factory;

final class LoadBookData extends Fixture implements DependentFixtureInterface
{
    private CommandBus $commandBus;

    private QueryBus $queryBus;

    public function __construct(CommandBus $commandBus, QueryBus $queryBus)
    {
        $this->commandBus = $commandBus;
        $this->queryBus = $queryBus;
    }

    public function load(ObjectManager $manager): void
    {
        $faker = Factory::create('pl_PL');

        for ($i = 0; $i < 1; $i++) {
            $command = new CreateBook($faker->sentence(3));
            $this->commandBus->dispatch($command);
        }
    }
}

CreateBook command looks like this:

final class CreateBook implements CommandInterface
{
    private UuidInterface $id;

    private string $title;

    public function __construct(string $title)
    {
        $this->id = Uuid::uuid4();
        $this->title = $title;
    }

    public function getId(): UuidInterface
    {
        return $this->id;
    }

    public function getTitle(): string
    {
        return $this->title;
    }
}

And handler for that command:

final class CreateBookHandler implements CommandHandlerInterface
{
    private Books $books;

    public function __construct(Books $books)
    {
        $this->books = $books;
    }

    public function __invoke(CreateBook $command)
    {
        $book = new Book(
          $command->getId(),
          $command->getTitle()
        );

        $this->books->add($book);
    }
}

After running this command: bin/console doctrine:fixtures:load -n
I get following error:

CRITICAL  [console] Error thrown while running command "doctrine:fixtures:load -n". Message: "An exception occurred while executing 'INSERT INTO book (title, id) VALUES (?, ?)' with params ["In illo reprehenderit in.", "8c6b9b43-57c9-4d0c-8310-cbe163d7acd4"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8c6b9b43-57c9-4d0c-8310-cbe163d7acd4' for key 'book.PRIMARY'" ["exception" => Doctrine\DBAL\Exception\UniqueConstraintViolationException^ { …},"command" => "doctrine:fixtures:load -n","message" => """  An exception occurred while executing 'INSERT INTO book (title, id) VALUES (?, ?)' with params ["In illo reprehenderit in.", "8c6b9b43-57c9-4d0c-8310-cbe163d7acd4"]:\n  \n  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8c6b9b43-57c9-4d0c-8310-cbe163d7acd4' for key 'book.PRIMARY'  """]

As you can see the id is uuid, so there is no chance for duplicate, but I get an error that it is duplicated.
May you let me know why I am getting this type of error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions