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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate entry for primary key 馃悰 #490

Closed
1 task done
peldax opened this issue Jul 7, 2024 · 4 comments
Closed
1 task done

Duplicate entry for primary key 馃悰 #490

peldax opened this issue Jul 7, 2024 · 4 comments
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug

Comments

@peldax
Copy link

peldax commented Jul 7, 2024

No duplicates 馃ゲ.

  • I have searched for a similar issue in our bug tracker and didn't find any solutions.

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
@peldax peldax added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Jul 7, 2024
@roxblnfk
Copy link
Member

roxblnfk commented Jul 8, 2024

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
Copy link
Author

peldax commented Jul 8, 2024

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
Copy link
Member

roxblnfk commented Jul 8, 2024

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
Copy link
Author

peldax commented Jul 11, 2024

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

@peldax peldax closed this as completed Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:to be verified Needs to be reproduced and validated. type:bug Bug
Projects
Status: Done
Development

No branches or pull requests

2 participants