Skip to content

Commit e48388e

Browse files
Fix PostgreSQL not-null violation when adding redirects (#619)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 439bd6f commit e48388e

4 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/Redirects/Eloquent/ErrorRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ private function toModel(Error $error): ErrorModel
4949
{
5050
$model = ErrorModel::find($error->id()) ?? new ErrorModel;
5151

52-
$model->id = $error->id();
52+
if (! is_null($error->id())) {
53+
$model->id = $error->id();
54+
}
55+
5356
$model->site = $error->site();
5457
$model->url = $error->url();
5558
$model->hits = $error->hits();

src/Redirects/Eloquent/RedirectRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ private function toModel(Redirect $redirect): RedirectModel
5252
{
5353
$model = RedirectModel::find($redirect->id()) ?? new RedirectModel;
5454

55-
$model->id = $redirect->id();
55+
if (! is_null($redirect->id())) {
56+
$model->id = $redirect->id();
57+
}
58+
5659
$model->site = $redirect->site();
5760
$model->source = $redirect->source();
5861
$model->destination = $redirect->destination();

tests/Redirects/Eloquent/ErrorRepositoryTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ public function can_delete_error()
8888
$this->assertDatabaseMissing('seo_pro_errors', ['id' => $error->id()]);
8989
}
9090

91+
#[Test]
92+
public function it_does_not_set_a_null_id_when_saving_a_new_error()
93+
{
94+
$error = Facades\Error::make()
95+
->url('/missing-page')
96+
->hits(1)
97+
->lastHitAt('2026-04-21 12:00:00');
98+
99+
$method = new \ReflectionMethod($this->repo, 'toModel');
100+
$model = $method->invoke($this->repo, $error);
101+
102+
$this->assertArrayNotHasKey('id', $model->getAttributes());
103+
}
104+
91105
#[Test]
92106
public function it_sets_default_site_when_saving_without_one()
93107
{

tests/Redirects/Eloquent/RedirectRepositoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ public function can_delete_redirect()
9696
$this->assertDatabaseMissing('seo_pro_redirects', ['id' => $redirect->id()]);
9797
}
9898

99+
#[Test]
100+
public function it_does_not_set_a_null_id_when_saving_a_new_redirect()
101+
{
102+
$redirect = Facades\Redirect::make()
103+
->source('/old-url')
104+
->destination('/new-url')
105+
->responseCode(302)
106+
->enabled(true);
107+
108+
$method = new \ReflectionMethod($this->repo, 'toModel');
109+
$model = $method->invoke($this->repo, $redirect);
110+
111+
$this->assertArrayNotHasKey('id', $model->getAttributes());
112+
}
113+
99114
#[Test]
100115
public function it_sets_default_site_when_saving_without_one()
101116
{

0 commit comments

Comments
 (0)