From e0c709ad5c67d55a50b88d7ad46ce3fe855c5b28 Mon Sep 17 00:00:00 2001 From: gouki Date: Mon, 7 Sep 2026 19:55:19 +0000 Subject: [PATCH] fix(import): null out orphan category references (FK safe) --- app/Console/Commands/SablogImport.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/SablogImport.php b/app/Console/Commands/SablogImport.php index a49f1e5..ff1a0dd 100644 --- a/app/Console/Commands/SablogImport.php +++ b/app/Console/Commands/SablogImport.php @@ -254,8 +254,14 @@ class SablogImport extends Command $status = (int) $row['visible'] === 1 ? 'published' : 'draft'; $dateline = ($ts = (int) $row['dateline']) > 0 ? date('Y-m-d H:i:s', $ts) : now()->toDateTimeString(); + // 老库存在已删除分类的孤儿文章;目标列可空,置 null + $categoryId = (int) $row['cid'] ?: null; + if ($categoryId !== null && ! DB::table('categories')->where('id', $categoryId)->exists()) { + $categoryId = null; + } + DB::table('posts')->updateOrInsert(['id' => (int) $row['articleid']], [ - 'category_id' => (int) $row['cid'] ?: null, + 'category_id' => $categoryId, 'user_id' => (int) $row['uid'] ?: null, 'title' => $row['title'], 'slug' => $slug,