~/VibeHandbook
免費 PDF

14 · 09

指揮 AI 構建你的資料層

AI 在 schema 設計和查詢編寫方面非常出色——前提是你給它約束去據此設計。不要說"做一個數據庫"。描述領域、關係,以及你不可妥協的底線。

一個強力提示看起來像這樣:

Design a PostgreSQL schema for a task-management app.

Entities and relationships:
- A user can own many projects.
- A project has many tasks.
- A task belongs to one project, has a status
  (todo / doing / done), and an optional due date.

Requirements:
- Use UUID primary keys and created_at timestamps.
- Enforce relationships with foreign keys.
- Add indexes for the columns we'll filter on.
- Give me the schema as SQL, plus a forward migration file.
- Explain any trade-offs you made.

然後用本章的清單來審查輸出:誠實的型別、外部索引鍵和約束齊備、合理的索引、沒有重複的資料。當你請求查詢時,明確要求引數化查詢,這樣使用者輸入就絕不會被拼接進 。而對於任何刪除或重新命名某物的遷移,讓 AI 把它點出來,這樣你可以先備份。

資料庫是你應用中最難撤銷錯誤的部分——資料一旦損壞或丟失,就不總能回來。這正是為什麼這些基本功值得了解:不是為了讓你自己寫出每一行,而是為了讓你能自信地指揮 AI,並在那些危險的操作上線之前抓住它們。

想離線閱讀?

免費下載整本書的 PDF 或 EPUB。