2018-11-06 11:37:17 +03:00
|
|
|
Setting default values for fields using Postgres defaults
|
|
|
|
=========================================================
|
2018-09-13 14:40:17 +03:00
|
|
|
|
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
|
|
|
You can set values of certain fields automatically when not explicitly passed to a fixed value, e.g. true for a boolean
|
|
|
|
field, or output of a simple SQL function, e.g. now() for a timestamp field, by setting column default values in the
|
|
|
|
table definition.
|
2018-09-13 14:40:17 +03:00
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The Postgres default value is ignored when a value is explicitly set to the field.
|
2018-09-13 14:40:17 +03:00
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
**Example:** Say we have a field ``created_at`` in a table ``article`` which we want to be set to the current
|
|
|
|
timestamp whenever a new row is added to the table:
|
2018-09-13 14:40:17 +03:00
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
Step 1: Modify the table
|
|
|
|
------------------------
|
2018-09-13 14:40:17 +03:00
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
Edit the ``created_at`` field and set its default value as the SQL function ``now()``.
|
2018-09-13 14:40:17 +03:00
|
|
|
|
|
|
|
Open the console and head to ``Data -> article -> Modify``:
|
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../../img/graphql/manual/schema/add-default-value.png
|
2020-01-08 16:20:18 +03:00
|
|
|
:alt: Modify the table in the console
|
2018-09-13 14:40:17 +03:00
|
|
|
|
|
|
|
.. admonition:: To set an auto-incrementing default value
|
|
|
|
|
|
|
|
To set a default value as an auto-incrementing integer you first need to set up a ``sequence`` which will be the
|
|
|
|
source of our default value.
|
|
|
|
|
|
|
|
Let's say we have a field called ``roll_number`` which we would like to be set by default as an auto-incremented
|
|
|
|
integer.
|
|
|
|
|
|
|
|
Head to ``Data -> SQL`` and run the following SQL command to create a new sequence.
|
|
|
|
|
|
|
|
.. code-block:: SQL
|
|
|
|
|
|
|
|
CREATE SEQUENCE roll_number_seq;
|
|
|
|
|
2018-10-04 07:09:47 +03:00
|
|
|
Now set the default value of the ``roll_number`` field as ``nextval('roll_number_seq')``.
|
2018-09-13 14:40:17 +03:00
|
|
|
|
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
Step 2: Run an insert mutation
|
|
|
|
------------------------------
|
2018-09-13 14:40:17 +03:00
|
|
|
|
|
|
|
Now if you do not pass the ``created_at`` field value while running an insert mutation on the ``article`` table, its
|
|
|
|
value will be set automatically by Postgres.
|
|
|
|
|
2019-03-13 13:03:45 +03:00
|
|
|
.. thumbnail:: ../../../../img/graphql/manual/schema/default-value-response.png
|
2020-01-08 16:20:18 +03:00
|
|
|
:alt: Run an insert mutation
|
2018-09-13 14:40:17 +03:00
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
Also see
|
|
|
|
--------
|
2018-09-13 14:40:17 +03:00
|
|
|
|
2018-11-06 11:37:17 +03:00
|
|
|
- :doc:`sql-functions`
|
2019-09-11 10:17:14 +03:00
|
|
|
- :doc:`column-presets`
|