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

Builder is not initialized when attaching entities in a many to many relation #635

Closed
federicoparroni opened this issue Apr 21, 2022 · 0 comments · Fixed by #636
Closed
Labels
bug An existing feature is not working as intended

Comments

@federicoparroni
Copy link
Contributor

Describe the bug
If you try to call the attach method to connect two model instances that are in a many to many relation, the following error is raised:

AttributeError: class model 'MyModel' has no attribute builder

To Reproduce

  1. Declare two models connected in a many to many relation:
    class Ssl(Model):
        __table__ = 'ssls'
        @belongs_to_many('ssl_id', 'app_id', 'id', 'id', table='ssl_app')
        def apps(self):
            from .app import App
            return App
     
     class App(Model):
        __table__ = 'apps'
        @belongs_to_many('app_id', 'ssl_id', 'id', 'id', table='ssl_app')
        def ssls(self):
            from .ssl import Ssl
            return Ssl
  2. Attach one entity to the other one:
    ssl = Ssl.find(1)
    app = App.find(2)
    
    ssl.attach('apps', app)   # Exception!
    # or simmetrically:
    app.attach('ssls', ssl)   # Exception!

This happens because the BelongsToMany class attach method access builder without checking whether it is initialized or not:

Pivot.on(current_model.builder.connection)

One workaround is to call get_builder() before calling attach:

ssl.get_builder()
ssl.attach('apps', app)   # now it works!

but I think the proper fix would be to change the BelongsToMany method in this way:

# Pivot.on(current_model.builder.connection) 
Pivot.on(current_model.get_builder().connection)

Expected behavior
The entities should be connected in the relation without issues.

Desktop (please complete the following information):

  • OS: Ubuntu
  • Version: 21.04

What database are you using?

  • Type: MySQL
  • Version: 14.14 Distrib 5.7.30, for Linux (x86_64)
  • Masonite ORM 2.6.1
@federicoparroni federicoparroni added the bug An existing feature is not working as intended label Apr 21, 2022
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.

1 participant