-
Notifications
You must be signed in to change notification settings - Fork 23.2k
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
[IMP] base: best practice for unique translated fields #164923
Draft
HydrionBurst
wants to merge
2
commits into
odoo:master
Choose a base branch
from
odoo-dev:master-core-unique-translation-cwg
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[IMP] base: best practice for unique translated fields #164923
HydrionBurst
wants to merge
2
commits into
odoo:master
from
odoo-dev:master-core-unique-translation-cwg
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
These 2 database_breaking tests try to get and lock the registry in the sub-threads while the main thread still holds the lock, which causes a deadlock problem. Mocking the lock while setting up the test should resolve this issue
This commit suggests the best practices for logical unique translated fields with the help of the test model `test_new_api.unique.translated.tags` `name1` field (unique index for the translated jsonb column) * only works for single language activated database `name2` field (python constraint) * cannot prevent duplicated records created/modified concurrently `name3` field (python constraint + unique index for column->>'en_US') * cannot prevent duplicated records modified concurrently The implementation of `name3` field is suggested.
HydrionBurst
force-pushed
the
master-core-unique-translation-cwg
branch
from
May 8, 2024 13:21
a2cd357
to
4a2c669
Compare
HydrionBurst
commented
May 14, 2024
Comment on lines
+2136
to
+2148
# python constraint | ||
name2 = fields.Char(translate=True, index='trigram') | ||
|
||
@api.constrains('name2') | ||
def _check_name2(self): | ||
self = self.filtered(lambda r: r.name2 is not False) | ||
if self._fields['name2'].index == 'trigram' and len(self) < 3: | ||
# use '=' to take advantage of the trigram index optimization | ||
record_num = self.search_count(['|'] * (len(self) - 1) + [('name2', '=', name) for name in self.mapped('name2')], limit=len(self) + 1) | ||
else: | ||
record_num = self.search_count([('name2', 'in', self.mapped('name2'))], limit=len(self) + 1) | ||
if record_num != len(self): | ||
raise ValidationError('Tag name must be unique') |
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.
python constraint (non-strict constraint)
Comment on lines
+2150
to
+2168
# python constraint + unique index ->>'en_US' | ||
name3 = fields.Char(translate=True, index='trigram') | ||
|
||
def _auto_init(self): | ||
result = super()._auto_init() | ||
tools.create_unique_index(self._cr, 'test_new_api_unique_translated_tag_name3_index', | ||
self._table, ["(name3->>'en_US')"]) | ||
return result | ||
|
||
@api.constrains('name3') | ||
def _check_name3(self): | ||
self = self.filtered(lambda r: r.name3 is not False) | ||
if self._fields['name3'].index == 'trigram' and len(self) < 3: | ||
# use '=' to take advantage of the trigram index optimization | ||
record_num = self.search_count(['|'] * (len(self) - 1) + [('name3', '=', name) for name in self.mapped('name3')], limit=len(self) + 1) | ||
else: | ||
record_num = self.search_count([('name3', 'in', self.mapped('name3'))], limit=len(self) + 1) | ||
if record_num != len(self): | ||
raise ValidationError('Tag name must be unique') |
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.
python constraint + unique constraint for ->>'en_US' (non-strict constraint)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit suggests the best practices for logical unique translated fields
with the help of the test model
test_new_api.unique.translated.tags
name1
field (unique index for the translated jsonb column)name2
field (python constraint)name3
field (python constraint + unique index for column->>'en_US')The implementation of
name3
field is suggested.Description of the issue/feature this PR addresses:
Current behavior before PR:
Desired behavior after PR is merged:
I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr