Consider usage of index.alter function (https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_index/#box-index-alter)
E.g. you want to add "id" field to your "user_id" index.
box.space.user.index.user_id:alter({
parts = {
{field = 'user_id', type = 'string'},
{field = 'id', type = 'unsigned'}}, -- Add "id" field to your index
})
or e.g. you want to add new field to your schema and then to schema (e.g. "name").
Firstly, you should update your format:
box.space.user:format({
{ name = 'id', type = 'unsigned'},
{ name = 'user_id', type = 'string'}, -- Preserve to existing fields
-- Add a new one
-- It's important to define them as nullable because
-- you don't have them physically
-- If you want to define them as non-nullable
-- You should append "name" to each tuple before
-- It's possible because format checks only defined fields
-- But basically tuple is a list of values and
-- it can have arbitrary length.
-- For simplification I say it as nullable.
{ name = 'name', type = 'string', is_nullable = true},
})
-- Then you can to alter your index
box.space.user.index.user_id:alter({
parts = {
{field = 'user_id', type = 'string'},
{field = 'name', type = 'string', is_nullable = true},
},
})
Also I see you use parts = {'id'}
and parts = {'user_id'}
. It's a correct way to define indexes - provide an array of fields that should be indexed. But it makes your schema less expressive and allows you to make a mistake. Instead of it I suggest you to use strict schema definition. - specify parts explicitly {field = <field_name/field_number>, type = <type>, collation = <collation>, is_nullable = <true/false>}
. You can omit type specification if format is already defined.
As shows the second example it's important. You can't specify parts={'user_id', 'name'}
with is_nullable=true
flag.