Skip to content

[IMP] util/models: check m2m tables on model rename #278

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aj-fuentes
Copy link
Contributor

When renaming a model we need to check m2m tables that may need to be
renamed as well. Otherwise the ORM will create a new table that would be
empty. If the data is handled directly in the scripts the ignore
parameter can be used to avoid warnings.

@robodoo
Copy link
Contributor

robodoo commented May 26, 2025

Pull request status dashboard

@aj-fuentes aj-fuentes requested a review from KangOl May 26, 2025 15:00
@aj-fuentes aj-fuentes force-pushed the master-imp_check_m2m-afu branch 3 times, most recently from ca53be3 to edc0489 Compare May 26, 2025 15:24
Copy link
Contributor

@KangOl KangOl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First quick review.

Comment on lines 310 to 312
m = re.match(r"(\w+)_{}_rel".format(old_table), m2m_table) or re.match(
r"{}_(\w+)_rel".format(old_table), m2m_table
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • one regex
  • escape the old name
Suggested change
m = re.match(r"(\w+)_{}_rel".format(old_table), m2m_table) or re.match(
r"{}_(\w+)_rel".format(old_table), m2m_table
)
m = re.match(r"^(\w+)_{0}_rel|{0}_(\w+)_rel$".format(re.escape(old_table)), m2m_table)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one regex

I had it originally like that. It makes the .group part a bit more complex but still OK.

escape the old name

Done.

m2m_tables = [t for t in get_m2m_tables(cr, new_table) if t not in ignore_m2m]
cr.execute(
"""
SELECT relation_table,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's available in all supported odoo versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 9, but I think we can let older versions (7 and 8) out of this given the low impact. It seems to be an issue mostly for latest versions.

r"{}_(\w+)_rel".format(old_table), m2m_table
)
if m:
new_m2m_table = "{}_{}_rel".format(*sorted([m.group(1), new_table]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, in old odoo versions, there wasn't any order. It wasn't always "{current_model}_{relation_model}" of the defining model. And the counterpart m2m definition had to explicitly define the relation table or a second m2m was created (and it happened).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 10 we order alphabetically, I think we can ignore 9 and 8.
https://github.com/odoo/odoo/blob/10.0/odoo/fields.py#L2335-L2338

@aj-fuentes aj-fuentes force-pushed the master-imp_check_m2m-afu branch 8 times, most recently from bb4bd77 to 5113080 Compare May 27, 2025 19:53
@aj-fuentes
Copy link
Contributor Author

upgradeci retry

@aj-fuentes
Copy link
Contributor Author

upgradeci skip

@aj-fuentes
Copy link
Contributor Author

upgradeci retry with always * in versions 10.0 11.0 12.0 saas~12.3 13.0 14.0 15.0 16.0 17.0 18.0

@aj-fuentes aj-fuentes force-pushed the master-imp_check_m2m-afu branch from 5113080 to 0b51946 Compare May 28, 2025 12:54
When renaming a model we need to check m2m tables that may need to be
renamed as well. Otherwise the ORM will create a new table that would be
empty. If the data is handled directly in the scripts the ignore
parameter can be used to avoid warnings.

Notes:
* From Odoo 9 the column relation_table exists in ir_model_fields
* From Odoo 10 the name of m2m tables is given by the model names ordered alphabetically

Related: odoo/upgrade#7752
@aj-fuentes aj-fuentes force-pushed the master-imp_check_m2m-afu branch from 0b51946 to bbb6a54 Compare May 30, 2025 20:24
@aj-fuentes
Copy link
Contributor Author

upgradeci retry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants