-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
MorphTo when changing db connection #29935
Comments
@klimby can you upgrade to 6.0 and see if the problem persists? I know quite some fixes to Eloquent have been made recently. |
Updated to 6.0. Unfortunately, the problem is relevant. |
I can reproduce this. |
It works well with MySQL. Is this issue only occur with Postgres driver? |
@adhyapranata The issue affects all databases. |
I couldn't reproduce this issue running Laravel 6.4.1 with MariaBD 10.3.16. |
@straube can you try mysql or postgres? |
@driesvints, @klimby, I traced the issue & found out that at @createModelByType, Eloquent lose track of the connection. This workaround fixed the issue : public function createModelByType($type)
{
$class = Model::getActualClassNameForMorph($type);
return (new $class)->setConnection($this->getConnection()->getName());
} but it messes up with @testMorphToRelationsAcrossDatabaseConnections, as the test expects the Morph models to work on different connections (so the MorphTo model should define its connection by its own not using the other model). This behavior seems totally right, as if we change it, then the Now I'm not sure if it's actually a bug or a planned feature. 🤔 |
@staudenmeir can you confirm the above? Is the current behavior correct? |
AFAICS, we need to mimic the behavior of all other relationships and only set the connection on the related model if the parent model is not using the default connection. framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php Lines 716 to 723 in 005cad6
This fixes the issue for me and doesn't break |
@staudenmeir you're right, that did work for me as well. |
Description:
MorphTo does not work when changing db connection.
Steps To Reproduce:
$model = Model::with('element')->first();
$model->element contains object.
$model = Model::on('B')->with('element')->first();
$model->element is null
$model = $model -> load('element');
$model->element is null
$element = $model -> element() -> first();
$element is object.
$model = Model::with('element')->first();
$model->element contains object.
$model = Model::on('A')->with('element')->first();
$model->element is null
The text was updated successfully, but these errors were encountered: