外部キーを作成する
todos
テーブルでは、 user_id
列の値が テーブルusers
の id
列にあるのが理想です。そうでなければ、一貫性のないデータが発生します。
Postgresでは、外部キー制約を定義してこの条件を強制できます。
テーブル todos
の user_id
列用のものを定義します。
コンソール -> DATA -> todos -> ページの編集に移動します。
以下のような画面が表示されます。
下部の Foreign Keys
セクションまでスクロールして、Add
をクリックします。
users
として参照テーブルを選択します。- 開始列を
user_id
として、終了列をid
として選択します。
todosテーブルのuser_id列は、ユーザーテーブルのidの値の1つでなければなりません。
Save
をクリックして外部キーを作成します。
完璧です。これで、データの一貫性が確保されました。
Did you find this page helpful?
Start with GraphQL on Hasura for Free
Build apps and APIs 10x faster
Built-in authorization and caching
8x more performant than hand-rolled APIs

