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

[IMP] base: best practice for unique translated fields #164923

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

HydrionBurst
Copy link
Contributor

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.

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

@robodoo
Copy link
Contributor

robodoo commented May 8, 2024

Pull request status dashboard.

@C3POdoo C3POdoo added the RD research & development, internal work label May 8, 2024
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 HydrionBurst force-pushed the master-core-unique-translation-cwg branch from a2cd357 to 4a2c669 Compare May 8, 2024 13:21
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')
Copy link
Contributor Author

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')
Copy link
Contributor Author

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
Labels
RD research & development, internal work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants