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

Question: ASUM in Many-to-Many relationship #14

Open
FelipeBarrosCruz opened this issue Nov 11, 2022 · 3 comments
Open

Question: ASUM in Many-to-Many relationship #14

FelipeBarrosCruz opened this issue Nov 11, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@FelipeBarrosCruz
Copy link

Hello folks, I have a question that I can't solve by myself.
I have a many-to-many relatioship as below:

  • products

    Column Type
    id UUID
    name text
    price decimal
  • orders

    Column Type
    id UUID
    user FK to directus_users table
  • orders_products

    Column Type
    id UUID
    order_id FK to orders table
    product_id FK to products table

image

I have a doubt how I can write a syntax to compute the field like this sql query:

SELECT
        SUM(p.price)
FROM
    orders AS o
INNER JOIN orders_products_1 AS op
    ON (o.id = op.orders_id)
INNER JOIN products AS p
    ON (op.products_id = p.id)

My goal is, when I add the product should calculate the total of order.

@FelipeBarrosCruz FelipeBarrosCruz changed the title ASUM in Many-to-Many relationship [Question] ASUM in Many-to-Many relationship Nov 11, 2022
@FelipeBarrosCruz FelipeBarrosCruz changed the title [Question] ASUM in Many-to-Many relationship Question: ASUM in Many-to-Many relationship Nov 11, 2022
@duydvu
Copy link
Contributor

duydvu commented Nov 12, 2022

Hi @FelipeBarrosCruz

I'm afraid it's not possible for now because ASUM can only handle second-level relational fields while your case is third-level. Fortunately, it's not hard to solve so if I have time, I will look into it.

@FelipeBarrosCruz
Copy link
Author

Hi @FelipeBarrosCruz

I'm afraid it's not possible for now because ASUM can only handle second-level relational fields while your case is third-level. Fortunately, it's not hard to solve so if I have time, I will look into it.

Hi @duydvu , thanks for replying.
Do you have some advice for me?
I want to help and contribute to your lib.

If you can explain what I need to do I'll code it.

Thank you again.

@duydvu
Copy link
Contributor

duydvu commented Nov 14, 2022

Contributions are appreciated.

You will need to specify which fields need to be queried in utils.ts.

Currently, no fields are specified in the query so Directus will return only top-level values (see here). If you know which fields are used in the template (require parsing it), then you can place them into the query and Directus will automatically return their values.

About how to parse the template, I'm using a simple parsing algorithm - find a pair of ( and ) recursively - in src/operations.ts. It has some limitations but can still identify which fields are used: if exp is not an expression (opMatch == false) nor a number literal, then it must be a field name.

Good luck!

@duydvu duydvu added the enhancement New feature or request label Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants