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

Unable to specify certain data to return from an association model #583

Closed
rzimmerman1 opened this issue Jan 28, 2022 · 3 comments · Fixed by #588
Closed

Unable to specify certain data to return from an association model #583

rzimmerman1 opened this issue Jan 28, 2022 · 3 comments · Fixed by #588
Labels
bug An existing feature is not working as intended

Comments

@rzimmerman1
Copy link

Describe the bug
A clear and concise description of what the bug is.
When adding a Object Model to associate with another model, you cannot specify what columns you want to return or even inherit from the Model it was being associated with.

Example:
my Group Model has a relationship "belongs to many" users
in my User model, i have

__hidden__ ["password", "api_token", "second_password", "remember_token", "created_at", "updated_at", "deleted_at"]
 @belongs_to_many("group_id", "user_id", "id", "id")
    def team(self):
        from app.models.User import User
        return User

with the result

 >>> group = Group.where('id', 2).first()
>>>  group.serialize()
{'id': 2, 'title': 'Team 1', 'user_id': 5, 'created_at': '2022-01-25T22:44:55+00:00', 'updated_at': '2022-01-25T22:44:55+00:00', 'deleted_at': None, 'team': [{'id': 3, 'email': 'johndoe@email.com', 'name': 'MurDock', 'password': 'bleh, 'second_password': None, 'remember_token': '017fcd11-511a-45b6-9225-ebf7a2810013', 'phone': None, 'verified_at': None, 'created_at': '2022-01-18T00:22:10+00:00', 'updated_at': '2022-01-18T00:22:10+00:00', 'deleted_at': None, 'api_token': 'bleh', 'group_user_id': 2, 'pivot': {'group_id': 2, 'user_id': 3, 'id': 1}},

I have tried to use get().pluck(colums....), .select(....)
with either an error or results that did not select the ones I wanted to return

Expected behavior
A possibility to allow the User collection to be manipulated based on selected columns to show
i.e.

>>> group = Group.where('id', 2).first()
>>>  group.serialize()
{'id': 2, 'title': 'Team 1', 'user_id': 5, 'created_at': '2022-01-25T22:44:55+00:00', 'updated_at': '2022-01-25T22:44:55+00:00', 'deleted_at': None, 'team': [{'id': 3, 'email': 'johndoe@email.com', 'name': 'MurDock', 'group_user_id': 2, 'pivot': {'group_id': 2, 'user_id': 3, 'id': 1}},

**What database are you using?**
 - Type: SQLite
 - Version 4
 - Masonite ORM 2.0
@rzimmerman1 rzimmerman1 added the bug An existing feature is not working as intended label Jan 28, 2022
@rzimmerman1
Copy link
Author

please let me know if its not clear and i can fix that. thank you

@Marlysson
Copy link
Contributor

@rzimmerman1 Can you provide your relationships mapped in your models ?

@rzimmerman1
Copy link
Author

Hi @Marlysson , sorry late to reply to your message.
if you mean the relationships of the db

 with self.schema.create("group_user") as table:
            table.increments("id")
            table.integer("group_id").index().nullable()
            table.foreign('group_id').references("id").on("groups").on_delete("cascade").nullable()
            table.integer("user_id").index().nullable()
            table.foreign('user_id').references("id").on("users").on_delete("cascade").nullable()
            table.soft_deletes()
            table.timestamps()

For the models, I am only calling the Group Model to get all users that are part of the group id.

from masoniteorm.models import Model
from masoniteorm.scopes import SoftDeleteScope
from masonite.authentication import Authenticates
from masonite.api.authentication import AuthenticatesTokens
from masoniteorm.relationships import has_many, belongs_to_many


class Group(Model):
    """Group Model"""
    __table__ = "groups"
    __fillable = ["title", "user_id"]

    __with__ = ['team']

    @belongs_to_many("group_id", "user_id", "id", "id")
    def team(self):
        from app.models.User import User
        return User


##### USER MODEL #####
class User(Model, SoftDeletesMixin, Authenticates, AuthenticatesTokens):
    """User Model."""

    __fillable__ = ["name", "email", "password"]
    __hidden__ = ["password", "api_token", "second_password", "remember_token", "created_at", "updated_at", "deleted_at"]
    __auth__ = "email"

    @has_many('id', 'user_id')
    def groups(self):
        return Group

Hope this helps

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