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

Can't lazy-load polymorphic relationships with different primary key columns #24654

Closed
fitztrev opened this issue Jun 21, 2018 · 1 comment
Closed

Comments

@fitztrev
Copy link
Contributor

  • Laravel Version: 5.6.26
  • PHP Version: 7.2.6
  • Database Driver & Version: MySQL 5.7.22

Description:

You cannot lazy-load models of a polymorphic relationship that have different primary key columns.

This functionality broke with #21310.

Scenario:

$comments = Comment::all();
$comments->load('commentable'); // this fails if you have the below schema

posts

post_id title body
111 Test Post hello world

videos

video_id title video
222 Test Video my-video.mov

comments

id commentable_type commentable_id body
1 App\Post 111 "comment on a post"
2 App\Video 222 "comment on a video"
// This works (eager loading):
$comments = App\Comment::with('commentable')->get();

// This fails (lazy loading):
$comments = App\Comment::all();
$comments->load('commentable');
// > Results in "no such column: videos.post_id"

Steps To Reproduce:

I created a failing integration test to demonstrate.

## Pull down my fork
git clone -b polymorphic-diff-keys git@github.com:fitztrev/framework.git polymorphic-diff-keys

cd polymorphic-diff-keys
composer install

## Run the tests
## These 2 work
./vendor/bin/phpunit --filter testWithNoEagerOrLazyLoad
./vendor/bin/phpunit --filter testItCanEagerLoad

## This 1 fails
./vendor/bin/phpunit --filter testItCanLazyLoad
@staudenmeir
Copy link
Contributor

#24063 has an explanation for this bug.

Currently, the only approach to fixing this is analyzing the backtrace. Since this is rather terrible, we are still looking for better ideas.

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

No branches or pull requests

2 participants