2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: Set default field values in Hasura
|
|
|
|
:keywords: hasura, docs, schema, default value
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
.. _default_field_values:
|
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
Setting default values for fields
|
|
|
|
=================================
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
Let's say you want certain fields to have their values set automatically when not explicitly passed. You can do this in
|
|
|
|
the following ways:
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
- :ref:`Postgres defaults <postgres_defaults>`: configure default values, using fixed values or simple SQL functions,
|
2019-09-11 10:17:14 +03:00
|
|
|
for columns in the table definition. E.g. an auto-incrementing ``id``, a ``created_at`` timestamp, etc.
|
2020-03-11 22:42:36 +03:00
|
|
|
- :ref:`Custom SQL functions <sql_functions_as_default>`: set up Postgres triggers which run custom SQL functions/stored procedures
|
2018-11-06 11:37:17 +03:00
|
|
|
to set the values of certain columns on inserts/updates on the table. This is useful to set values of fields which
|
|
|
|
depend on other fields passed in the input. e.g. set ``submission_time`` of an online quiz as 1 hour from the
|
|
|
|
``start_time``.
|
2020-03-11 22:42:36 +03:00
|
|
|
- :ref:`Role based column presets <column_presets>`: set up presets, using session variables or fixed values, that are
|
|
|
|
applied when a new row is created with a particular :ref:`user role <roles_variables>`.
|
2019-09-11 10:17:14 +03:00
|
|
|
E.g. set a ``user_id`` field automatically from a session variable/authorization header.
|
2020-07-14 11:03:17 +03:00
|
|
|
- :ref:`Created_at / updated_at timestamps <created_updated_timestamps>`: set up ``created_at`` and ``updated_at`` timestamp values.
|
2018-11-06 11:37:17 +03:00
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 1
|
|
|
|
:hidden:
|
|
|
|
|
|
|
|
Postgres defaults <postgres-defaults>
|
|
|
|
Custom SQL functions/stored procedures <sql-functions>
|
|
|
|
Role based column presets <column-presets>
|
2020-07-14 11:03:17 +03:00
|
|
|
Created_at / updated_at timestamps <created-updated-timestamps>
|
2018-11-06 11:37:17 +03:00
|
|
|
|