Skip to content

[10.3] Morph one of many query error  #46394

Closed
@dwightwatson

Description

@dwightwatson
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions