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
Add Pivot Table support (belongs_to_many) #322
Comments
There is actually currently no way to work with many to many tables at the moment through a pivot table. morph_to is related to polymorphic tables |
Changing this ticket from a question to a feature request |
Feature SpecificationNeed to add support for We should be able to have a table structure like the following:
We should then be able to go from In the models it should look like this: from masoniteorm.relationships import belongs_to_many
class Store(Model):
@belongs_to_many
def products(self):
from app.models import Product
return Product It should also be assumed that the pivot table is The keys order should be reversed as well:
The SQL for generating the result set should be as follows: SELECT products.*
FROM stores
JOIN product_store ON product_store.store_id = stores.id
JOIN products ON product_store.product_id = products.id
WHERE stores.id = 1 So by default this should look something like: from masoniteorm.relationships import belongs_to_many
class Store(Model):
@belongs_to('store_id', 'product_id', 'id', 'id')
def products(self):
from app.models import Product
return Product We must be able to default these keys Since we are going directly to the related table and skipping the pivot table we have no real way to get to the pivot. Because of this we should add an attribute on the model to get the pivot. By default this will be class Store(Model):
@belongs_to('store_id', 'product_id', 'id', 'id').as('product_pivot')
def products(self):
from app.models import Product
return Product We can then access the pivot table using: store = Store.find(1)
store.product_pivot Lastly these results should return a collection of results The relationship class should be a standalone class done inside the relationships module. The tables can be setup and tested in the sqlite database |
@circulon Took me all weekend to make sure I added all the features of other ORM's but this is now in release 1.0.7 https://orm.masoniteproject.com/models#has-many-many-to-many |
Awesome Work!! Thanks for being so responsive!! |
@circulon welcome :) |
I see that in Orator there is a facility for working with Pivot Tables
https://orator-orm.com/docs/0.9/orm.html#working-with-pivot-table
Is this a feature of Masonite Orm using the current decorators? or maybe @morph_to (which i'm not sure I understand anyway)
My investigations have only pointed out that I will have a fun time making the traversal work.
Any pointers/snippets would be greatly appreciated
The text was updated successfully, but these errors were encountered: