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

Value changed in creating observer is not saved in the database #707

Closed
JRubics opened this issue Jun 3, 2022 · 5 comments · Fixed by #712
Closed

Value changed in creating observer is not saved in the database #707

JRubics opened this issue Jun 3, 2022 · 5 comments · Fixed by #712
Labels
bug An existing feature is not working as intended

Comments

@JRubics
Copy link
Contributor

JRubics commented Jun 3, 2022

Hi, I am using observer like this:

class WebsiteObserver:
    def creating(self, website):
        website.app_name = new_name()

Website.observe(WebsiteObserver())

but after I do:

website = Website()
...
website.save()

observer is called, but value is not saved in the database. If I try to call website.get_raw_attribute('app_name') after save() I get the new value, but just website.app_name returns the old one.

Expected behavior
I think it should change value to the new one, but please check if I am doing something wrong.

Desktop:

OS: Linux
Version Ubuntu 21.10

What database are you using?

Type: [MySQL]
Version [14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper]
Masonite ORM [2.4.2](https://app.tara.ai/68/*/tasks/masonite-x-inc)(https://app.tara.ai/68/*/tasks/masonite-x-inc)(https://app.tara.ai/68/*/tasks/masonite-x-inc)<br />[Tara Task]<br />[Tara Task]<br />[Tara Task]<br />[Tara Task](https://app.tara.ai/masonite-x-inc/*/tasks/68)
@JRubics JRubics added the bug An existing feature is not working as intended label Jun 3, 2022
@josephmancuso
Copy link
Member

cannot replicate this behavior. Could you give some more detail? what is the value of new_name()?

Heres what i tried to replicate:

  1. observer:

Screen Shot 2022-06-05 at 3 40 20 PM

  1. Code

Screen Shot 2022-06-05 at 3 41 48 PM

  1. Result

Screen Shot 2022-06-05 at 3 42 22 PM

Looks like its working. It's also working if i don't set the name, its also working if i use the create method.

Can you tell me what else is missing that i haven't tried here?

@JRubics
Copy link
Contributor Author

JRubics commented Jun 6, 2022

I realized I am using with DB.transaction(): and that's why just website.app_name is not working until transaction get executed. Sorry, my bad 😞

I still can't make it work even after the transaction. Transaction looks like this:
tr7
I'm updating vhost after save() because it depends on new app_name value. After this, in the database app_name still has old value.

I managed to solve it doing this:
tr6
but I don't think it is the right way. Do you know what am I doing wrong?

One more note: new_name() is making a query to the database to check if name already exists. If it does, it adds suffix to the name. I'm not sure if that query has something to do with the transaction?

And thank you a lot for helping here 🙏

@josephmancuso
Copy link
Member

hmm possibly. This is deff more good information so I'll go back and see if I can replicate this

@josephmancuso josephmancuso changed the title Value changed in creating ovserver is not saved in the database Value changed in creating observer is not saved in the database Jun 6, 2022
@JRubics
Copy link
Contributor Author

JRubics commented Jun 6, 2022

Thank you! :)

@tara-ai tara-ai bot changed the title Value changed in creating observer is not saved in the database Value changed in creating observer is not saved in the database # Jun 6, 2022
@tara-ai tara-ai bot changed the title Value changed in creating observer is not saved in the database # Value changed in creating observer is not saved in the database [P1](https://app.tara.ai/68/*/tasks/masonite-x-inc) Jun 6, 2022
@tara-ai tara-ai bot changed the title Value changed in creating observer is not saved in the database [P1](https://app.tara.ai/68/*/tasks/masonite-x-inc) Value changed in creating observer is not saved in the database Jun 6, 2022
@tara-ai tara-ai bot changed the title Value changed in creating observer is not saved in the database Value changed in creating observer is not saved in the database [P1](https://app.tara.ai/68/*/tasks/masonite-x-inc) Jun 6, 2022
@josephmancuso josephmancuso changed the title Value changed in creating observer is not saved in the database [P1](https://app.tara.ai/68/*/tasks/masonite-x-inc) Value changed in creating observer is not saved in the database Jun 8, 2022
@josephmancuso
Copy link
Member

v2.13.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants