Fix : Type Mismatch in Polymorphic Relationships When Using PostgreSQL #54401#58603
Draft
bazan101 wants to merge 7 commits intolaravel:12.xfrom
Draft
Fix : Type Mismatch in Polymorphic Relationships When Using PostgreSQL #54401#58603bazan101 wants to merge 7 commits intolaravel:12.xfrom
bazan101 wants to merge 7 commits intolaravel:12.xfrom
Conversation
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.
shaedrich
reviewed
Feb 4, 2026
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); |
Contributor
There was a problem hiding this comment.
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); |
Author
There was a problem hiding this comment.
yeah it is a better implementation but don't we want to to keep the parent::addConstraints(); line at the begining ?
Contributor
There was a problem hiding this comment.
we don't and we don't keep it
…es relations morphiques et met à jour les tests associés.
added 4 commits
February 4, 2026 13:30
… le formatage dans un test MorphMany et ajoute le fichier de configuration Gemini.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have implemented a fix for the PostgreSQL type mismatch issue in polymorphic relationships #54401 .
Summary of Changes:
Modified
MorphOneOrMany.php: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.
Laravel from optimizing for integers when the foreign key column might be a string.
Updated Tests (
DatabaseEloquentMorphTest.php):testMorphManyEagerConstraintsAreProperlyAdded to expect whereIn with string values (e.g.,
['1', '2']) instead of integers.
with string values ('1') instead of integers.
Verification:
bug (expectations failed with integers) and verify the fix (expectations met with strings).
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).