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

compute does not work with emmett relations #358

Closed
Triquetra opened this issue Jun 22, 2021 · 4 comments · Fixed by #394
Closed

compute does not work with emmett relations #358

Triquetra opened this issue Jun 22, 2021 · 4 comments · Fixed by #394
Assignees
Milestone

Comments

@Triquetra
Copy link

With this table definition (I can post the complete sample code if it would be helpful):

class Tmodel(Model):
    has_many('tas', {'amodels': {'via': 'tas'}})
    has_many('tbs', {'bmodels': {'via': 'tbs'}})

    a = Field()
    # total_avg_cost = Field.decimal(10, 2, auto_validation=False)

    @rowmethod('total_avg_cost')
    def get_avg_cost(self, row):
        avg_list = [lst.my_avg() for lst in row.bmodels.select(db.Tb.ALL) if lst.my_avg()]
        return sum(avg_list) / len(avg_list) if avg_list else 0

Tmodel.first().total_avg_cost() works as a rowmethod (returns 0 when no related records).

But when using @compute instead, Tmodel.create(a='foo') fails to create the record, with an 'Invalid Value' error.

If setting auto_validation=False on the total_avg_cost field, the record is created with total_avg_cost=None instead of the computed 0 (when there are no related records).

@gi0baro
Copy link
Member

gi0baro commented Jun 24, 2021

@Triquetra so, I admit I wrote unclear documentation about this, or at least this is an unexpected behaviour caused by pyDAL actually.

The reality is that while rowattr and rowmethod actually receive the real row instance, the compute receive an OpRow object, since the computation logic is still handled by pyDAL library through this method, and consequentially you don't have access to relations' attributes, like row.bmodels.

So my answer to this is: I should quickly fix the documentation specifying the difference, and then probably re-engineer the computation logic overriding the one by pyDAL to use Emmett ORM objects. The re-engineering will obviously take a while.

@gi0baro
Copy link
Member

gi0baro commented Jun 24, 2021

@Triquetra side note: this is true also for callbacks accepting existing records, like before_update, but probably the documentation is better there since the parameter is named fields instead of row.

@gi0baro
Copy link
Member

gi0baro commented Jun 24, 2021

Partially related: #174, #197

@Triquetra
Copy link
Author

Thank you for the clarification. At least now I know I'm not crazy (well, at least not for that reason)! Thank you also for all of your hard work on emmett.

@Triquetra Triquetra changed the title rowmethod works where compute does not compute does not work with emmett relations Jun 25, 2021
@gi0baro gi0baro added enhancement and removed bug labels Jul 23, 2021
@gi0baro gi0baro added this to the 2.4 milestone Aug 11, 2021
@gi0baro gi0baro mentioned this issue Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants