space_object:format() | Tarantool
Submodule space_object:format()


object space_object

Declare field names and types.

  • space_object (space_object) – an object reference
  • format-clause (table) – a list of field names and types

nil, unless format-clause is omitted

Possible errors:

  • space_object does not exist;
  • field names are duplicated;
  • type is not legal.

Ordinarily Tarantool allows unnamed untyped fields. But with format users can, for example, document that the Nth field is the surname field and must contain strings. It is also possible to specify a format clause in

The format clause contains, for each field, a definition within braces: {name='...',type='...'[,is_nullable=...]}, where:

  • the name value may be any string, provided that two fields do not have the same name;
  • the type value may be any of allowed types: any | unsigned | string | integer | number | boolean | array | map | scalar, but for creating an index use only indexed fields;
  • the optional is_nullable value may be either true or false (the same as the requirement in “Options for space_object:create_index”). See also the warning notice in section Allowing null for an indexed key.

It is not legal for tuples to contain values that have the wrong type; for example after{{' ',type='number'}}) the request{'string-which-is-not-a-number'} will cause an error.

It is not legal for tuples to contain null values if is_nullable=false, which is the default; for example after{{' ',type='number',is_nullable=false}}) the request{nil,2} will cause an error.

It is legal for tuples to have more fields than are described by a format clause. The way to constrain the number of fields is to specify a space’s field_count member.

It is legal for tuples to have fewer fields than are described by a format clause, if the omitted trailing fields are described with is_nullable=true; for example after{{'a',type='number'},{'b',type='number',is_nullable=true}}) the request{2} will not cause a format-related error.

It is legal to use format on a space that already has a format, thus replacing any previous definitions, provided that there is no conflict with existing data or index definitions.

It is legal to use format to change the is_nullable flag; for example after{{' ',type='scalar',is_nullable=false}}) the request{{' ',type='scalar',is_nullable=true}}) will not cause an error – and will not cause rebuilding of the space. But going the other way and changing is_nullable from true to false might cause rebuilding and might cause an error if there are existing tuples with nulls.


There are legal variations of the format clause:

  • omitting both ‘name=’ and ‘type=’,
  • omitting ‘type=’ alone, and
  • adding extra braces.

The following examples show all the variations, first for one field named ‘x’, second for two fields named ‘x’ and ‘y’.{{'x'}}){{'x'},{'y'}}){{name='x',type='scalar'}}){{name='x',type='scalar'},{name='y',type='unsigned'}}){{name='x'}}){{name='x'},{name='y'}}){{'x',type='scalar'}}){{'x',type='scalar'},{'y',type='unsigned'}}){{'x','scalar'}}){{'x','scalar'},{'y','unsigned'}})

The following example shows how to create a space, format it with all possible types, and insert into it.

--- ...
         >                     {name='2',type='unsigned'},
         >                     {name='3',type='string'},
         >                     {name='4',type='number'},
         >                     {name='5',type='integer'},
         >                     {name='6',type='boolean'},
         >                     {name='7',type='scalar'},
         >                     {name='8',type='array'},
         >                     {name='9',type='map'}})
--- ...
--- ...
tarantool>{{'a'},      -- any
         >                    1,          -- unsigned
         >                    'W?',       -- string
         >                    5.5,        -- number
         >                    -0,         -- integer
         >                    true,       -- boolean
         >                    true,       -- scalar
         >                    {{'a'}},    -- array
         >                    {val=1}}    -- map
- [['a'], 1, 'W?', 5.5, 0, true, true, [['a']], {'val': 1}]

Names specified with the format clause can be used in space_object:get() and in space_object:create_index() and in tuple_object[field-name] and in tuple_object[field-path]

If the format clause is omitted, then the returned value is the table that was used in a previous space_object:format(format-clause) invocation. For example, after{{'x','scalar'}}), will return [{'name': 'x', 'type': 'scalar'}].

Note re storage engine: vinyl supports formatting of non-empty spaces. Primary index definition cannot be formatted.

Found what you were looking for?