Foreign Key
外部キー
外部キーはデータベースのルールです。1つの列が同じテーブルまたは別のテーブルの有効なキーを指すようにします。`null` はその列に選択されたルールに従います。
もう一つの例
すべての `invoice_line.invoice_id` は既存の請求書を指さなければなりません。下書きを削除するとその行も削除されますが、投稿済みの請求書は削除できず、逆仕訳のプロセスが必要です。バルクインポートでは、外部キーを永久に無効にするのではなく、問題のある行を別に保持します。
主な部分
- 01参照列
- 02参照候補キー
- 03更新/削除アクション
以下の場合に使用します
データベースのリレーションが親を欠いてはいけない場合は、外部キーを使用します。更新および削除のアクションを慎重に選択してください。
以下の場合は使用しないでください
並行処理の場合、アプリのチェックだけに頼らないでください。カスケード削除が境界を超える前に、データ保持と監査義務を確認してください。
コードで使用する名前
FOREIGN KEY (…) REFERENCES … ON UPDATE/DELETE … 出荷前に
Check contracts and invariants, authorization, and input limits. Test retries, one-time effects, timeouts, and partial failures. Verify data integrity, compatibility, monitoring, recovery, and rollback.
Outcome:Complete the task with 外部キー (Foreign Key)。 User context:すべての `invoice_line.invoice_id` は既存の請求書を指さなければなりません。下書きを削除するとその行も削除されますが、投稿済みの請求書は削除できず、逆仕訳のプロセスが必要です。バルクインポートでは、外部キーを永久に無効にするのではなく、問題のある行を別に保持します。 Technical concept or control:外部キー (Foreign Key)。 Why it fits:データベースのリレーションが親を欠いてはいけない場合は、外部キーを使用します。更新および削除のアクションを慎重に選択してください。 Do not use it when:並行処理の場合、アプリのチェックだけに頼らないでください。カスケード削除が境界を超える前に、データ保持と監査義務を確認してください。 Engineering requirements: Define inputs, outputs, contracts, and rules that must always hold. Set authorization and input checks. Define failures, timeouts, retries, one-time effects, data moves, and compatibility limits. Operations: Add useful signals and logs that hide sensitive data. Set capacity limits and write an operator guide. Test recovery and rollback. Acceptance criteria: Contract and failure-path tests pass. Data and authorization rules still hold during retries and partial failures. Check performance limits, monitoring, recovery, and rollback. Evidence and limits (evidence boundary):その情報源にとって公式です。他のプラットフォームやチームはその用語を別の方法で使用する場合があります。 Unknowns to confirm: Where the code runs, data stores, traffic shape, consistency needs, and the security boundary. Also confirm the compatibility period, service goals, and rollback limits.
このリクエストを確認する
Review how 外部キー (Foreign Key) is used now. Definition: 外部キーはデータベースのルールです。1つの列が同じテーブルまたは別のテーブルの有効なキーを指すようにします。`null` はその列に選択されたルールに従います。 Release checks: Check contracts and invariants, authorization, and input limits. Test retries, one-time effects, timeouts, and partial failures. Verify data integrity, compatibility, monitoring, recovery, and rollback. Before changing code, list the evidence, gaps, severity, and the smallest safe fix.