question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot update a model instance in a many to many relation

See original GitHub issue

Describe the bug Updating a model instance involved in a many to many relation throws the following exception:

'(1054, "Unknown column \'mytable.pivot\' in \'field list\'")'

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. Update an entity:
    app = App.find(2)
    
    for ssl in app.ssls:
       # custom logic to decide if update or not
       ssl.my_column = my_value
       ssl.save()   # Exception!
    

Expected behavior Model instance should be updated without issues.

Screenshots or code snippets The generated query is:

"UPDATE `ssls` SET `ssls`.`pivot` = '?', `ssls`.`my_column` = '?' WHERE `ssls`.`id` = '?'"

with the following param bindings:

0:<masoniteorm.models.Pivot.Pivot object at 0x7f656007ebf0>
1:None
2:54

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
josephmancusocommented, May 2, 2022

Ok ill review it when you do 😃

0reactions
stoutputcommented, May 16, 2022

@federicoparroni Can this one be closed now? #649 has been merged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update manytomany field in Django? - Stack Overflow
In SQL is really easy to do. If id of "George" = 3 and id of "Georfe" = 7 and the relation table...
Read more >
Many-to-many relationships - Django documentation
To define a many-to-many relationship, use ManyToManyField . In this example, an Article can be published in multiple Publication objects, and a Publication ......
Read more >
Making queries — Django 4.1.4 documentation
While it can be customized , by default you refer to a “reverse” relationship in a lookup using the lowercase name of the...
Read more >
The right way to use a ManyToManyField in Django
To maintain a many-to-many relationship between two tables in a database, the only way is to have a third table which has references...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
Inserting & Updating Related Models. The save Method; The create Method; Belongs To Relationships; Many To Many Relationships. Touching Parent Timestamps ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found