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

[10.3] Morph one of many query error #46394

Closed
dwightwatson opened this issue Mar 8, 2023 · 2 comments · Fixed by #46402
Closed

[10.3] Morph one of many query error #46394

dwightwatson opened this issue Mar 8, 2023 · 2 comments · Fixed by #46402

Comments

@dwightwatson
Copy link
Contributor

  • Laravel Version: 10.3.1
  • PHP Version: 8.2.3
  • Database Driver & Version: Postgres 14.7

Description:

My test suite blew up in the upgrade from 10.2.0 to 10.3.0/10.3.1.

I have Property which has many Image with an order. I use morphOne()->ofMany() to fetch properties with their primary image (i.e. with the minium order).

This worked fine on 10.2.0 but on 10.3.0 I get a query error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "images.order" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...oin (select min("images"."id") as "id_aggregate", "images"."... ^ (Connection: pgsql, SQL: select "images".* from "images" inner join (select min("images"."id") as "id_aggregate", "images"."order" as "order_aggregate", "images"."imageable_id", "images"."imageable_type" from "images" inner join (select min("images"."order") as "order_aggregate", "images"."imageable_id", "images"."imageable_type" from "images" where "images"."imageable_id" in (...) and "images"."imageable_type" = App\Models\Property group by "images"."imageable_id", "images"."imageable_type") as "image" on "image"."order_aggregate" = "images"."order" and "image"."imageable_type" = "images"."imageable_type" and "image"."imageable_id" = "images"."imageable_id" group by "images"."imageable_id", "images"."imageable_type") as "image" on "image"."id_aggregate" = "images"."id" and "image"."order_aggregate" = "images"."order" and "image"."imageable_type" = "images"."imageable_type" and "image"."imageable_id" = "images"."imageable_id")

Here's the basic model setup;

class Property extends Model
{
    public function image()
    {
        return $this->morphOne(Image::class, 'imageable')
            ->ofMany('order', 'min');
    }

    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }
}

class Image extends Model
{
    public function imageable()
    {
        return $this->morphTo();
    }
}

And the query that blows up;

Property::with('image')->simplePaginate();

As a sanity check I tested this against SQLite and it's a non issue. Didn't test against MySQL but I presume it may be Postgres specific.

Steps To Reproduce:

I've replicated this issue in a new Laravel app: https://github.com/dwightwatson/bug-report

@dwightwatson
Copy link
Contributor Author

Looking through the changes between the two tagged versions it looks like #46309 could be the cause.

@Guilhem-DELAITRE
Copy link
Contributor

Looking through the changes between the two tagged versions it looks like #46309 could be the cause.

Totally is, I added a comment in it, asking a temporary rollback because I can't fix it right now.

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 a pull request may close this issue.

2 participants