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

Data duplication on hasManyTrough with pivot #205

Closed
psust opened this issue Sep 14, 2023 · 3 comments
Closed

Data duplication on hasManyTrough with pivot #205

psust opened this issue Sep 14, 2023 · 3 comments

Comments

@psust
Copy link

psust commented Sep 14, 2023

I have a

persons table and a properties table. They have a many to many relationship. So I have a table person_property.

Person::class belongs to User::class which belongs to Tenant::class.

How would you get all the properties of a Tenant ?

I tried:

<?php

return $this->hasManyDeep(Property::class, [User::class, Person::class, "person_property"]);

But this returns me duplicate properties since I have a many to many relationship.

I tried appending to the relationship: ( like stated in #2 )

<?php

->distinct("properties.id")

# I also tried

->groupBy("properties.id")

Neither of these worked.

I tried using a query instead of a relationship, but I faced the same problem of duplication.

@staudenmeir
Copy link
Owner

staudenmeir commented Sep 16, 2023

Hi @psust,

Neither of these worked.

Are the queries failing with an error or are the duplicate (just) not getting removed?

@Robert-Smagge
Copy link

Robert-Smagge commented Oct 10, 2023

We also had some trouble with data duplication, encountering this error:

Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #... of SELECT list is not in GROUP BY clause and contains nonaggregated column ... which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

We solved it by adding 'laravel_through_key' to the offending ->groupBy. Would that solve your issue as well?

@psust
Copy link
Author

psust commented Oct 11, 2023

Hey, sorry I forgot about this. I believe the duplication issue came from a problem with our database.

Anyway I have this now and it seems to work:

<?php

return $this->hasManyDeep(Property::class, [User::class, Person::class, "person_property"])
    ->distinct("properties.id");

@psust psust closed this as completed Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants