Closed
Description
Product: Tarantool
Since: 3.0
Root document:
- https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/format/
- https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#constraint-functions
Depends on: #3520
Details
Reference
Root document: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/format/
The format clause contains, for each field, a definition within braces:
{name='...',type='...'[,default=...][,default_func=...]}
,
where:
- (Optional) The
default_func
string value specifies the name of a
function, that is used to generate a default value for the field. If
default_func
is set, thedefault
value is used as the function
argument. See field default functions.
Concepts
Root document: https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#constraint-functions
Field default functions
Stored Lua function can be used to generate a default value for the tuple
field. It can take one optional parameter, and must return exactly one
value. To create a field default function, use func.create with function
body. The function must not yield.
Example:
box.schema.func.create('random_point', {
language = 'Lua',
body = 'function(param) return math.random(param.min, param.max) end'
})
box.schema.space.create('test')
box.space.test:create_index('pk')
box.space.test:format({
{name = 'id', type = 'integer'},
{name = 'latitude', type = 'number',
default_func = 'random_point', default = {min = -90, max = 90}},
{name = 'longitude', type = 'number',
default_func = 'random_point', default = {min = -180, max = 180}}
})
tarantool> math.randomseed(os.time())
---
...
tarantool> box.space.test:insert{1}
---
- [1, 56, 38]
...
Requested by @Gumix in tarantool/tarantool@b055625.
Definition of done
- Reference: document the
default_func
value in the space format - Concepts: add
Field default functions
section