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

with_ lamda filters not working. #765

Closed
yubarajshrestha opened this issue Jul 15, 2022 · 3 comments · Fixed by #766
Closed

with_ lamda filters not working. #765

yubarajshrestha opened this issue Jul 15, 2022 · 3 comments · Fixed by #766
Labels
bug An existing feature is not working as intended

Comments

@yubarajshrestha
Copy link
Contributor

Imagine you have list of products with many to many relation with categories.

Below code works fine

products = Product.with_("categories").get()
[
    {
        "id": 1,
        "name": "Product 1",
        "categories": [
            {
                "id": 1,
                "name": "Category 1",
                "status": "published"
            },
            {
                "id": 2,
                "name": "Category 2",
                "status": "draft"
            }
        ]
    }
]

Filtering with_ lambda (Doesn't work)

products = Product.with_("categories", lambda query: (
    query.where("status", "published")
)).get()

Expected Result

[
    {
        "id": 1,
        "name": "Product 1",
        "categories": [
            {
                "id": 1,
                "name": "Category 1",
                "status": "published"
            },
        ]
    }
]

Actual Result

[
    {
        "id": 1,
        "name": "Product 1",
        "categories": [
            {
                "id": 1,
                "name": "Category 1",
                "status": "published"
            },
            {
                "id": 2,
                "name": "Category 2",
                "status": "draft"
            }
        ]
    }
]
@yubarajshrestha yubarajshrestha added the bug An existing feature is not working as intended label Jul 15, 2022
@josephmancuso
Copy link
Member

belongs_to_many

@yubarajshrestha
Copy link
Contributor Author

belongs_to_many

Model class looks like this:

"""CommerceProduct Model."""
class Product(Model):
    """Product Model."""
    __primary_key__ = "id"

    __fillable__ = [
        #...
    ]

    @belongs_to_many(
        local_foreign_key="product_id",
        other_foreign_key="category_id",
        table="category_product",
    )
    def categories(self):
        """Returns all categories for this product."""
        from ..models.Category import Category

        return Category

@josephmancuso
Copy link
Member

I could only get this to work with the belongs to if you did this:

products = Product.with_("categories", lambda query: (
    query.where("categories.status", "published")
)).get()

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