Closed chelout closed 4 years ago
@driesvints if you confirm it's a bug, i can make a pr upon my proposal.
@chelout I'll need more time to figure this out. I think you're correct but not sure.
@driesvints No problem, just let me know.
The fix would break DatabaseEloquentModelTest::testPushOneRelation()
and testPushManyRelation()
, but I'm not entirely sure whether these tests actually make sense in their current form.
What they basically test:
$user = new User();
$user->setRelation('posts', new \Illuminate\Database\Eloquent\Collection([new Post]));
$user->push();
At the moment, push()
saves both the user and the related post. But it doesn't set the foreign key, so the post ends up with user_id => null
.
What would be a scenario where an unsaved related model makes sense?
I can only think of this rather theoretical example:
$user = User::first();
$post = new Post(['user_id' => $user->id]);
$user->setRelation('posts', new \Illuminate\Database\Eloquent\Collection([$post]));
$user->push();
I'm not really sure I consider this a bug based on the current implementation and purpose of push.
Description:
Recently i encountered a bug when duplicating model with
withDefault()
relation, that raises Illuminate\Database\QueryException. Someone could say that it is a feature, but i'm confident it's a bug.Steps To Reproduce:
Let's create two models:
Deal
Transaction
Filling data:
Proposal:
We can add one more check for such situation to Model's push method like so:
Added check for related model existance.