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

Fix: ambigious queries when adding other tables to the query builder #887

Merged
merged 1 commit into from
Nov 26, 2024

Conversation

adminfriso
Copy link
Contributor

@adminfriso adminfriso commented Nov 26, 2024

when joining in a HasMany relation, the orderBy only targets the column name, and does not select the table, which will result in ambigious errors.

This PR focuses the table, which does not influence "normal" queries, but it will help when joining in other tables.

Old situation, source of PR:

Rule.php (Rule model)


/**
     * Perform a search against the model's indexed data.
     */
    public static function search($query = '', $callback = null)
    {
        return static::parentSearch($query, $callback)->query(function (Builder $builder): void {
            $builder
                ->join('rule_versions', 'rules.id', '=', 'rule_versions.rule_id');
        });
    }

(Resulting issue)

SQLSTATE[42702]: Ambiguous column: 7 ERROR: ORDER BY "id" is ambiguous LINE 1: ...like $5) and "rules"."deleted_at" is null order by "id" desc ^


select
  *
from
  "rules"
  inner join "rule_versions" on "rules"."id" = "rule_versions"."rule_id"
where
  (
    "rules"."id":: text ilike % asdf %
    or "rules"."code":: text ilike % asdf %
    or "rule_versions"."summary":: text ilike % asdf %
    or "rule_versions"."is_published":: text ilike % asdf %
    or "rule_versions"."id":: text ilike % asdf %
  )
  and "rules"."deleted_at" is null
order by
  "id" desc

@taylorotwell taylorotwell merged commit a17af14 into laravel:10.x Nov 26, 2024
19 checks passed
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

Successfully merging this pull request may close these issues.

2 participants