-
-
Notifications
You must be signed in to change notification settings - Fork 173
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
[FIX] m2o_to_x2m: fix dup key insertion #381
Conversation
f8f826b
to
2398d2f
Compare
Today I wanted to reference the project_hr script and just see that @aleuffre removed it telling it was buggy OCA/project@5e35249 This fix in openupgradelib might still be interesting for other such situations. I let you guys be the judges. |
hi @rvalyi. I'm not sure to understand. Do you understand the content of your PR ? Thanks ! |
@legalsylvain I didn't know EXCEPT myself, but it seems it does the job. In the precise project_hr v15 migration script, understand that because of the computed field (see my previous link), the script was not required and "buggy". But still the m2o_to_x2m openupgradelib method has currently no guard against inserting duplicates and this may still be an interesting feature to have in the method. Thus it would only insert missing relations instead of failing hard if some relation was already inserted in the table previously; no matter how. At this point I'm afraid chatGPT will explain the query better than me (the end is near for us :-) )
This part of the query selects all the records from the project_task table where the employee_id is not null. The selected columns are id (which represents project_task_id) and employee_id. The EXCEPT clause compares the results of the two SELECT statements and returns only those rows that are present in the first SELECT but not in the second. This part selects all the existing records from the hr_employee_project_task_rel table. If a row from the first SELECT exists in the second SELECT, it will be excluded from the result, meaning it won't be inserted again, preventing duplicates. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The only way I see for having duplicates is because you are inserting records in an existing m2m table, which is a case you must handle other way. If not, a many2one (one value) is not able to produce duplicates when inserting it in the m2m table. Can you explain the use case where it produces duplicates? The Closing for now, unless a valid use case arises. |
Hello, while trying to migrate a production database to v15, during the project_hr post-migration script (the one removed here OCA/project@5e35249):
I got this error:
I asked chatGPT how I could avoid inserting duplicates and it suggested me this fix with EXCEPT. With this fix I got my migration working and the relation table was populated:
cc @aleuffre @pilarvargas-tecnativa @pedrobaeza