如何在 Tarantool 中使用自增索引?

vk2*_*k26 3 tarantool

我做了自动增量索引:

box.space.metric:create_index('primary', {
        parts = {{'id', 'unsigned'}},
        sequence = true,
})
Run Code Online (Sandbox Code Playgroud)

然后我尝试在id字段中传递 nil :

metric.id = nil
Run Code Online (Sandbox Code Playgroud)

当我尝试插入这个值时,我发现错误:

Tuple field 1 type does not match one required by operation: expected unsigned
Run Code Online (Sandbox Code Playgroud)

我必须为自动增量字段传递什么值?

第二个问题。如果我使用 tarantool-cluster 的实例很少(例如基于墨盒应用程序),是否证明使用自动增量索引?会不会出现不同实例有重复键的情况?

Mon*_*son 6

不可能通过nil。当您分配时nil,您将擦除字段。用box.NULL来代替。但更好的是,使用某种集群 id,它在集群间表现良好,而不是仅在一个节点内工作的自动增量。

对于集群范围的 id,我可以提出UUID或类似的ULID(例如来自https://github.com/moonlibs/id