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

Filter Relations: Ambiguous column reference error #25

Open
nhatkha1407 opened this issue Nov 8, 2023 · 2 comments
Open

Filter Relations: Ambiguous column reference error #25

nhatkha1407 opened this issue Nov 8, 2023 · 2 comments

Comments

@nhatkha1407
Copy link

nhatkha1407 commented Nov 8, 2023

First of all, thank you to the author and contributors for creating this useful package!

I ran into the following error when using the package:

Endpoint:

/posts?filters[collections][id][$eq]=1

Post.php

public function collections(): BelongsToMany
{
    return $this->belongsToMany(
        PostCollection::class,
        'post_collections_ref_posts'
    )->withPivot(['position'])->withTimestamps();
}

Error:

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column `id` in where clause is ambiguous (Connection: mysql, SQL: select count(*) as aggregate from `posts` where exists (select * from `post_collections` inner join `post_collections_ref_posts` on `post_collections`.`id` = `post_collections_ref_posts`.`post_collection_id` where `posts`.`id` = `post_collections_ref_posts`.`post_id` and `id` = 1) and `posts`.`deleted_at` is null) in file /vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 822

The query needs to disambiguate the ambiguous id column reference.

-- Current
...`post_collections_ref_posts`.`post_id` and `id` = 1

-- Expected: Specify the table alias for relation columns
...`post_collections_ref_posts`.`post_id` and `post_collections`.`id` = 1

I'm happy to provide any other details that would be helpful.

Again, thanks for your work on this package!

@abbasudo
Copy link
Owner

abbasudo commented Nov 9, 2023

hello, glad that purity helped you.
purity at it core uses laravels whereHas (laravel docs) function to filter by relations. try whereHas function outside purity and see if it works. you probably need to redefine the belongsToMany relation.

@nhatkha1407
Copy link
Author

What do you think if we manual add table name inside whereHas

$query->whereHas($field, function ($subQuery) use ($callback) {
$this->applyRelations($subQuery, $callback);
});

I tried implementing this in the EqualFilter.php file and it seems to work well. Adding the table name provides more clarity in the query and ensures we are filtering on the correct table.

Current:

foreach ($this->values as $value) {
$query->where($this->column, $value);
}

Modified:

    foreach ($this->values as $value) {
        $query->where($query->from . '.' . $this->column, $value);
    }

Let me know if you have any other thoughts!

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

2 participants