Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate primary key (uuid) #352

Open
chrisooo3 opened this issue May 23, 2021 · 1 comment
Open

Duplicate primary key (uuid) #352

chrisooo3 opened this issue May 23, 2021 · 1 comment

Comments

@chrisooo3
Copy link

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?

@fd6130
Copy link

fd6130 commented Jun 21, 2022

How about using Symfony Uuid component?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants