cycle / orm

PHP DataMapper, ORM
https://cycle-orm.dev
MIT License
1.21k stars 71 forks source link

Duplicate entry for primary key šŸ› #490

Closed peldax closed 1 week ago

peldax commented 2 weeks ago

No duplicates šŸ„².

What happened?

Hi,

I am encountering a strange issue, where the ORM tries to re-insert a row into a database. I have a following code:

        $template = $repository->findByPK(1);
        var_dump($template->getTemplateBlocks()->first()->getId()); // prints 1
        var_dump($template->getTemplateBlocks()->last()->getId()); // prints 2

        $exam = new Exam();
        $exam->setTemplate($template);
        $exam->setName('Test');
        $entityManager->persist($exam)->run();

This code results into a following error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

/var/www/html/vendor/cycle/database/src/Driver/Driver.php:460
/var/www/html/vendor/cycle/database/src/Driver/Driver.php:286
/var/www/html/vendor/cycle/database/src/Query/InsertQuery.php:125
/var/www/html/vendor/cycle/orm/src/Command/Database/Insert.php:131
/var/www/html/vendor/cycle/orm/src/Transaction/Runner.php:61
/var/www/html/vendor/cycle/orm/src/Transaction/UnitOfWork.php:150
/var/www/html/vendor/cycle/orm/src/Transaction/UnitOfWork.php:327
/var/www/html/vendor/cycle/orm/src/Transaction/UnitOfWork.php:374
/var/www/html/vendor/cycle/orm/src/Transaction/UnitOfWork.php:219
/var/www/html/vendor/cycle/orm/src/Transaction/UnitOfWork.php:96
/var/www/html/vendor/cycle/orm/src/EntityManager.php:47

The templateBlocks is a hasMany relation in template entity. The exam has a belongs to relation to the template.

When dumping the insert queries in InsertQuery::run(), the ORM indeed tries to execute a following SQL:

INSERT INTO `template_block` (`id`, `template_id`, `grading_id`, `name`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

The computed set of changes in the Tuple is not an empty array, although the row has been re-fetched by the repository in the first step.

My schema is generated from annotations, but I havent found any issues there. Everything seems to be generated correctly.

Do you have any idea what might be the cause? I tried to find the cause of this issues myself, but I am clueless.

Thanks.

Version

Cycle packages: latest
PHP: 8.3.8
roxblnfk commented 2 weeks ago

Hi. First, make sure that the ORM used for querying and the ORM used in EntityManager are the same ORM object. In rare cases they may be different, for example when the Cycle\ORM\ORM class is requested from DI rather than Cycle\ORM\ORMInterface.

peldax commented 2 weeks ago

Hi, thanks for your response. Both services are loaded from DI, but I confirm that spl_object_id of ORM is always the same.

roxblnfk commented 2 weeks ago

Could you prepare a test case in ORM to have a reproducible sample? The instruction: https://cycle-orm.dev/docs/issue-test-case/current/en

peldax commented 1 week ago

Hi, this one was probably an issue on my side and was resolved locally.