Skip to content

Fix : Type Mismatch in Polymorphic Relationships When Using PostgreSQL #54401#58603

Draft
bazan101 wants to merge 7 commits intolaravel:12.xfrom
bazan101:fix/postgres-morph-type-mismatch
Draft

Fix : Type Mismatch in Polymorphic Relationships When Using PostgreSQL #54401#58603
bazan101 wants to merge 7 commits intolaravel:12.xfrom
bazan101:fix/postgres-morph-type-mismatch

Conversation

@bazan101
Copy link

@bazan101 bazan101 commented Feb 3, 2026

I have implemented a fix for the PostgreSQL type mismatch issue in polymorphic relationships #54401 .

Summary of Changes:

  1. Modified MorphOneOrMany.php:

    • Overridden addConstraints() to strictly cast the parent model's key to a string before
      adding the where clause. This ensures that the generated SQL uses strict string binding
      (e.g., '1' instead of 1), which is required by PostgreSQL when comparing against varchar
      columns.
    • Overridden addEagerConstraints() to:
      • Explicitly use whereIn instead of whereIntegerInRaw (or other variations), preventing
        Laravel from optimizing for integers when the foreign key column might be a string.
      • Strictly cast all parent keys to strings when binding to the query.
  2. Updated Tests (DatabaseEloquentMorphTest.php):

    • Updated testMorphOneEagerConstraintsAreProperlyAdded and
      testMorphManyEagerConstraintsAreProperlyAdded to expect whereIn with string values (e.g.,
      ['1', '2']) instead of integers.
    • Updated helper methods (getOneRelation, getManyRelation, etc.) to expect where clauses
      with string values ('1') instead of integers.

Verification:

  • Created a temporary test suite DatabaseEloquentMorphOneOrManyPostgresFixTest to confirm the
    bug (expectations failed with integers) and verify the fix (expectations met with strings).
  • Ran the existing test suite DatabaseEloquentMorphTest.php and ensured all tests pass with the
    updated expectations.

This solution ensures compatibility with PostgreSQL's strict typing while maintaining
functionality for other databases (where string-to-int comparison is often implicit).

Benjamin Azan added 2 commits February 3, 2026 13:55
- Cast parent keys to strings in MorphOneOrMany relations for PostgreSQL compatibility (laravel#54401).
- Updated MorphOneOrMany tests to expect string keys and parameterized SQL.
- Fixed Mockery warnings in tests.
Comment on lines 58 to +64
$this->getRelationQuery()->where($this->morphType, $this->morphClass);

parent::addConstraints();
$this->getRelationQuery()->where(
$this->foreignKey, '=', (string) $this->getParentKey()
);

$this->getRelationQuery()->whereNotNull($this->foreignKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just do this?

Suggested change
$this->getRelationQuery()->where($this->morphType, $this->morphClass);
parent::addConstraints();
$this->getRelationQuery()->where(
$this->foreignKey, '=', (string) $this->getParentKey()
);
$this->getRelationQuery()->whereNotNull($this->foreignKey);
$this->getRelationQuery()
->where($this->morphType, $this->morphClass)
->where($this->foreignKey, '=', (string) $this->getParentKey())
->whereNotNull($this->foreignKey);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it is a better implementation but don't we want to to keep the parent::addConstraints(); line at the begining ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't and we don't keep it

…es relations morphiques et met à jour les tests associés.
@bazan101 bazan101 marked this pull request as draft February 4, 2026 12:26
Benjamin Azan added 4 commits February 4, 2026 13:30
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