2

有没有办法命名类似于 SQL 中的列的字段?

这个想法是我可能想插入一条客户记录: - 姓名 - 电话 - 电子邮件 - 网站

有些字段有时可能会出现,有些则不会,而且它们可能会以不同的顺序出现。

有没有其他方法可以将记录插入到通过字段名称引用它们的元组中?

伪代码中的一些东西,例如:

s:insert(1, "name": "foo name", "phone": "foo phone")
s:insert(2, "phone": "bar phone", "name": "bar name", "email": "bar email")
4

1 回答 1

2

当您为空间定义架构时,您可以使用尚未记录space:format()的函数分配字段名称,然后您可以将这些名称用于索引定义。[available in Tarantool 1.7+]

示例代码:

box.once("testapp:schema:1", function()
    local customer = box.schema.space.create('customer')
    customer:format({
        {'customer_id', 'unsigned'},
        {'name', 'string'},
    })
    customer:create_index('customer_id', {parts = {'customer_id'}})

    local account = box.schema.space.create('account')
    account:format({
        {'account_id', 'unsigned'},
        {'customer_id', 'unsigned'},
        {'balance', 'unsigned'},
        {'name', 'string'},
    })
    account:create_index('account_id', {parts = {'account_id'}})
    account:create_index('customer_id', {parts = {'customer_id'}, unique = false})
    box.snapshot()
end)

不幸的是,您不能在space:insert()或类似函数中使用字段名称。

于 2017-12-12T14:10:16.567 回答