# 檔名 \initdb\17168erp_schema.sql : 結構SQL \initdb\include : 初始DB**需要**的TABLE (12個) \initdb\exclude : 初始DB**不要**的TABLE (55個) 目前為範例資料 --- \initdb\db_init.sql : 只含include的資料列 \initdb\db_demo.sql : 含少數exclude的範例資料列 ## 假資料範圍(筆數) 信眾(50?), 報名(200?), 帳戶(1?) 法會, 功德: 應可用真資料? 其它: 真假資料都不要? 如: 寮房? 掛單? # Table名稱及建立順序 (依外鍵關聯排序,include在前) ### ====== Batch 1: 無外鍵 ====== #### include admin_group company country files item PostCity PostNumber AncestralTabletStatus RegionRoomBedStatus RegionType stock_reason #### exclude GuadanTimeSetting accounting_kind accounting_kind2 actItem_kind activity_category_kind activity_kind appellation bed_kind member_group member_title news_kind project_kind stock_kind supplier_kind ### ====== Batch 2 ====== #### include admin #### exclude actItem followers activity supplier bed_kind_detail AncestralTabletArea Region ### ====== Batch 3 : exclude only ====== member pro_order activity_check family_members followers_tablet GuaDanOrder act_bom actItem_files activity_kind_detail activity_relating activity_spares news AncestralTabletPosition Room admin_log ### ====== Batch 4 : exclude only ====== pro_order_detail member_check project news_files AncestralTabletRegistrant RegionRoomBed ### ====== Batch 5 : exclude only ====== bed_order accounting stock project_sub AncestralTabletPositionRecord GuaDanOrderGuest ShuWen ### ====== Batch 6 : exclude only ====== bed_order_detail accounting_files stock_files transfer_register RegionAndRoomAndBedSchedule ### ====== Batch 7 ====== pro_order_record # 執行錯誤分析 ## 問題 1:自引用外鍵的 INSERT 順序錯誤 (68 筆錯誤) ### 受影響的表 | 表 | 自引用外鍵 | 錯誤數 | |---|---|---| | Region | FK_Region_ParentUuid (ParentUuid → Uuid) | 4 | | followers | FK_followers_followers (leader → num) | 1 | ### 範例:Region.Table.sql 順序錯誤 ``` 第1行:三樓男眾寮房,ParentUuid = 455... → 引用第3行 ❌ 第2行:十樓,ParentUuid = 1cc... → 引用第5行 ❌ 第3行:三樓,ParentUuid = 1cc... → 引用第5行 ❌ 第4行:十樓女眾寮房,ParentUuid = 629... → 引用第2行 ❌ 第5行:鄉根大樓,ParentUuid = NULL ✓ (根節點) ``` ### 解決方案 重新排序 SQL 檔案,根節點在前: ``` 鄉根大樓 (根) → 三樓, 十樓 → 三樓男眾寮房, 十樓女眾寮房 ``` --- ## 問題 2:連鎖外鍵錯誤 ### 原因 自引用外鍵的表(Region, followers)因 INSERT 順序錯誤導致部分記錄插入失敗, 進而導致引用這些表的子表也出現外鍵錯誤。 ### 為什麼源資料庫中這些資料能存在? 源資料庫可能: 1. 外鍵用 `WITH NOCHECK` 創建(不檢查現有資料) 2. 插入時暫時禁用外鍵約束 3. 導出時只導出部分資料(有過濾條件) 但我們的 schema 使用 `WITH CHECK`(嚴格模式),每筆 INSERT 都會驗證。 ### 錯誤鏈 ``` Region 部分記錄插入失敗 (自引用順序錯誤) └→ Room 外鍵錯誤 └→ RegionRoomBed 外鍵錯誤 └→ GuaDanOrderGuest 外鍵錯誤 └→ RegionAndRoomAndBedSchedule 外鍵錯誤 (14筆) followers 部分記錄插入失敗 (自引用順序錯誤) └→ pro_order (~20筆), family_members, followers_tablet 外鍵錯誤 ``` ### 結論 **修正 Region 和 followers 的 INSERT 順序後,連鎖錯誤應會消失。** ### 解決方案 1. 修正自引用表的 INSERT 順序 2. 或在插入前禁用外鍵檢查,插入後再啟用 --- ## 已解決:UTF-8 編碼問題 `initdb.ps1` 已加上 `-f 65001` 參數 --- # 待辦工作清單 ## 自引用外鍵表分析 | 表 | 自引用外鍵 | 資料狀況 | 需處理? | |---|---|---|---| | act_bom | package_num → num | 已按順序排列 | ❌ | | AncestralTabletArea | ParentAreaId → AreaId | 空表 | ❌ | | followers | leader → num | 1 筆錯誤 | ⚠️ 待查 | | Region | ParentUuid → Uuid | 4 筆錯誤,順序錯誤 | ✅ 需重排 | ## 工作項目 - [ ] 重排 `exclude/dbo.Region.Table.sql` INSERT 順序 - [ ] 檢查 `exclude/dbo.followers.Table.sql` 中 leader 引用問題 - [ ] 重新測試初始化腳本