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
Cannot update a model instance in a many to many relation #637
Comments
Thanks. Looks like the pivot table name is being overridden by the defaults instead of the one specified using the |
actually looks like theres just an extra random |
Hi @josephmancuso. Thank you for the prompt reply. |
Hello @josephmancuso! Can you please share any idea/suggestion that can help solving this issue so I can try to work on it? |
Thanks for the interest in fixing this. I believe the root of the issue is this block of code here https://github.com/MasoniteFramework/orm/blob/2.0/src/masoniteorm/relationships/BelongsToMany.py#L142-L149 what this is doing is it is setting the pivot object as on the model (typically named as then when you go to save it, since we set the pivot attribute it is trying to save with the I THINK the solution is to actually just this this manually on the model.__original_attributes__.update({self._as: (Pivot.on(query.connection)
.table(self._table)
.hydrate(pivot_data)
.activate_timestamps(self.with_timestamps))}) Something like that. That would be the first place I start looking on this issue A little more low level but I would imagine that this prevents the |
While debugging my specific case, it appears that this code is never reached: https://github.com/MasoniteFramework/orm/blob/2.0/src/masoniteorm/relationships/BelongsToMany.py#L142-L149 I think the problem is in here: orm/src/masoniteorm/models/Model.py Line 707 in a23458b
In the self.__dirty_attributes__ there is also the pivot attribute. Few lines before that, I see that we remove the builder from the __dirty_attributes__ :
if "builder" in self.__dirty_attributes__:
self.__dirty_attributes__.pop("builder") Can we pop also for the |
@federicoparroni no we can't do that. That code is in the model and the query builder is tightly coupled to the model so its ok to pop the builder there. The relationship classes are not tightly coupled to the models so we need to find another way. Somewhere it is being set on the model so we just need to find where and modify that |
@josephmancuso Yes, you are right. I am currently trying to find out the exact point where the pivot is set in the |
Checking ;) |
@josephmancuso I think I should have fixed the issue. I'm going to open a PR! |
Ok ill review it when you do :) |
@federicoparroni Can this one be closed now? #649 has been merged. |
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
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:
Desktop (please complete the following information):
What database are you using?
The text was updated successfully, but these errors were encountered: