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

Translations broken on postgresql if using UUIDs #360

Open
pixelplant opened this issue Aug 3, 2023 · 2 comments
Open

Translations broken on postgresql if using UUIDs #360

pixelplant opened this issue Aug 3, 2023 · 2 comments

Comments

@pixelplant
Copy link

Describe the bug
After the upgrade to 11.12 (since we need Laravel 10 support) our translations are broken. The main table using translations has an UUID key used for the getTranslationRelationKey(), so imagine having a table called "properties" with an "uuid" primary key and the translations are stored in property_translations.

To Reproduce
Make sure you're using a postgresql DB
Use any eloquent model that has an uuid primary key, let's say it's called Property. Call the translations method on it:

$p = App\Models\Property::first()
$p->translation

and see the following error:

SQLSTATE[42883]: Undefined function: 7 ERROR:  function max(uuid) does not exist
LINE 1: ...\" from \"property_translations\" inner join (select max(\"prope...

Expected behavior
Translations should work as before

Screenshots
If applicable, add screenshots to help explain your problem.

Versions (please complete the following information)

  • PHP: 8.1
  • Database: Postgres 12 or 14
  • Laravel: 10
  • Package: 11.12.1

Additional context
It's linked to the "translation" call in the Relationship.php trait
New code with error calls 'max' on the ofMany relationship

    public function translation(): HasOne
    {
        return $this
            ->hasOne($this->getTranslationModelName(), $this->getTranslationRelationKey())
            ->ofMany([
                $this->getTranslationRelationKey() => 'max',
            ], function (Builder $query): void {
                $query->where($this->getLocaleKey(), $this->localeOrFallback());
            });
    }

Old code which worked did not call max and worked fine

    public function translation(): HasOne
    {
        return $this
            ->hasOne($this->getTranslationModelName(), $this->getTranslationRelationKey())
            ->where($this->getLocaleKey(), $this->localeOrFallback());
    }

Exception

function max(uuid) does not exist
LINE 1: ...\" from \"property_translations\" inner join (select max(\"prope...

Stack Trace
The full stack trace of the thrown exception.

@pixelplant pixelplant added the bug label Aug 3, 2023
@pixelplant
Copy link
Author

see the notes regarding the MAX aggregate and UUIDs on PostgreSQL => https://laravel.com/docs/10.x/eloquent-relationships#has-one-of-many

@Gummibeer
Copy link
Member

Not a bug with the package - as you noted it's based on the max call which isn't supported on PostgreSQL UUIDs. So don't use UUIDs or don't use the translation relationship.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants