2

Is there any way to name fields similar to columns in SQL?

The idea is that I might want to insert a customer record with: - Name - Phone - Email - Website

some fields might be present sometimes, other not, and they might be presented in different order.

Is there any other way to insert the records into a tuple referencing them by field name?

Something in pseudocode like:

s:insert(1, "name": "foo name", "phone": "foo phone")
s:insert(2, "phone": "bar phone", "name": "bar name", "email": "bar email")
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • You may use avro schemas to define schemas for your spaces. There is very good tutorial here: https://tarantool.org/en/doc/1.7/book/app_server/creating_app.html. – Vladimir Lebedev Dec 09 '17 at 07:08
  • Thanks, I am currently using Marshmallow (as far as I know it is similar to Avro), I was just wondering if there would be something to assign names to fields same as SQL columns have names. – M.E. Dec 09 '17 at 10:47

1 Answers1

2

You can assign field names using not yet documented space:format() function when you define schema for a space, afterwards and you can use these names for index definitions. [available in Tarantool 1.7+]

Sample code:

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)

Unfortunately, you can't use field names in space:insert() or similar functions.

Vladimir Lebedev
  • 1,207
  • 1
  • 11
  • 25