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

table_exists should not return SQL views #303

Open
diggy128 opened this issue Sep 7, 2022 · 6 comments
Open

table_exists should not return SQL views #303

diggy128 opened this issue Sep 7, 2022 · 6 comments

Comments

@diggy128
Copy link

diggy128 commented Sep 7, 2022

table_exists returns all database objects that match the name provided.
This includes:

  • ordinary table
  • index
  • sequence
  • TOAST table
  • view
  • materialized view
  • composite type
  • foreign table
  • partitioned table
  • partitioned index

as per relkind in documentation

This fails to run when upgrading a module that has a model defined with view (_auto=False).

Query should limit the object type to table (aka relkind = v)

@diggy128 diggy128 added the bug label Sep 7, 2022
@pedrobaeza
Copy link
Member

Well, right now it's used for detecting an object in the DB and act accordingly (meaning for example that a module is installed), so as is, it's correct. If you want such specific use, then an extra argument for restricting the search or a new function should be implemented, but this can't be changed, as that would mean to break existing scripts relying on this behavior.

@diggy128
Copy link
Author

diggy128 commented Sep 7, 2022

I understand that changing current behavior would break existing scripts but shouldn't that be addressed?

In my case migration failed because a model based on a view had a reference field and Open upgrade tried to rename the referenced model.

Anyway, shouldn't we at some point define functions to make explicit checks for objects Odoo uses and deprecate the current function?

Should you agree, I'll be happy to help with a PR.

@pedrobaeza
Copy link
Member

I have already stated that an extra method or argument may be acceptable.

@legalsylvain
Copy link
Contributor

hi @diggy128. Did you planned to do a PR for that extra feature ?

@StefanRijnhart
Copy link
Member

@diggy128 I would be open to changing the behaviour slightly to fit the name more properly, but I'm curious to learn more about the problem you encountered. What do you mean with "a model based on a view had a reference field and Open upgrade tried to rename the referenced model", and how did the current behaviour of rename_table fit into this?

@diggy128
Copy link
Author

diggy128 commented Oct 14, 2022

@StefanRijnhart That is what I was thinking. We have a method named table_exists but essentially looks for many other database objects.

Now in my case:
I have an abstract model model that has a reference field:

class PartnerBalanceReport(models.AbstractModel):
    _name = "partner.balance.report"
    _auto = False

ref = fields.Reference(selection=_links_get, string='Reference', size=128)

And two others that inherit it. One of them is:

class PartnerCustomerBalanceReport(models.Model):
    _name = "partner.customer.balance.report"
    _auto = False
    _inherit = ['partner.balance.report']

The inherited models instantiate a separate SQL view passing parameters to a base query from the abstract class.
The ref field is calculated in SQL and used as a reference field in Odoo (aka model,id)

Let's assume here that there is a row with a calculated ref value of account.invoice,123456
Also lets assume that in v13 it should be mapped to account.move,789123

This worked great but when trying to run OpenUpgrade, table_exists returns True even though this is a view and not a table.
Since table_exists is used in various places in OpenUpgrade to facilitate migration, when hitting the ref field, (not knowing it is a non updatable view) it tries to update the field value in the table from account.invoice,123456 to account.move,789123.

Since the table is non updatable an error occurs.
Since it's been while, I don't have the trial migration logs to check the error but if you dim it critical I could cook up a demo DB to replicate and report.

So the problem occurs when migrating the reference field but stems from the fact that table_exists returns True when it should return False. This is around L901 in openupgrade.py

Ideally as I mentioned above we should define separate functions to make explicit checks for objects Odoo uses and fix the current function (to reflect the function name).

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

No branches or pull requests

4 participants