Create Foreign Key
In the todos
table, the value of user_id
column must be ideally present in the id
column of users
table. Otherwise, it would result in inconsistent data.
Postgres allows you to define foreign key constraint to enforce this condition.
Let's define one for the user_id
column in todos
table.
Head over to Console -> DATA -> todos -> Modify page.
It should look something like this:
Scroll down to Foreign Keys
section at the bottom and click on Add
.
- Select the Reference table as
users
- Choose the From column as
user_id
and To column asid
We are enforcing that the user_id
column of todos
table must be one of the values of id
in users
table.
Click on Save
to create the foreign key.
Great! Now you have ensured data consistency.
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