2

I need to call auto_increment function in Tarantool 1.6 using python client.

I have tried without success:

database = tarantool.connect("localhost", 3301)
s = database.space("customer")
s.call('auto_increment','foo')

Could someone clarify how to insert a new record with 'foo' as field using auto_increment in python?

I include the error message, I tried several ways to use auto_increment in Python without success.

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nameko/containers.py", line 388, in _run_worker
    result = method(*worker_ctx.args, **worker_ctx.kwargs)
  File "./service.py", line 25, in create
    self.server.call('box.auto_increment', (0, 'foo'))
  File "/usr/local/lib/python2.7/dist-packages/tarantool/connection.py", line 373, in call
    response = self._send_request(request)
  File "/usr/local/lib/python2.7/dist-packages/tarantool/connection.py", line 341, in _send_request
    return self._send_request_wo_reconnect(request)
  File "/usr/local/lib/python2.7/dist-packages/tarantool/connection.py", line 261, in _send_request_wo_reconnect
    response = Response(self, self._read_response())
  File "/usr/local/lib/python2.7/dist-packages/tarantool/response.py", line 87, in __init__
    raise DatabaseError(self._return_code, self._return_message)
DatabaseError: (48, 'Unknown request type 10')
M.E.
  • 4,955
  • 4
  • 49
  • 128

2 Answers2

2

First of all, you should use tarantool 1.7+ instead of 1.6. Depending on your setup, you should either install newer version of Tarantool using a package manager for your operating system, or use a corresponding docker image, i.e.:

$ docker run --rm -p 3301:3301 -t -i tarantool/tarantool:1.7

Run the following code in tarantool console:

box.cfg{ listen=3301 }
customer = box.schema.space.create('customer', { 
    if_not_exists=true, 
    temporary=true 
})
customer:create_index('primary', {parts = {1, 'unsigned' }})

Now, run python and execute the following:

$ python
>> import tarantool
>> server = tarantool.connect("localhost", 3301)
>> space = server.space("customer")
>> space.call("box.space.customer:auto_increment", [['alpha']])
- [1, 'alpha']
>> space.call("box.space.customer:auto_increment", [['bravo']])
- [2, 'bravo']

Notice the two-dimesional array in arguments for space.call().

Since version 1.7 auto_increment() is deprecated and the proper way to have autoincremented index is to use sequences.

Restart your tarantool and execute the following lua code in tarantool console:

box.cfg{ listen=3301 }

customer = box.schema.space.create('customer', {
    if_not_exists=true,
    temporary=true
})

box.schema.sequence.create('S', { min=1 })

customer:create_index('primary', {
    parts = {1, 'unsigned' },
    sequence = 'S'
})

Now, run python and execute the following:

$ python
>> import tarantool
>> server = tarantool.connect("localhost", 3301)
>> space = server.space("customer")
>> space.insert((None, "alpha"))
- [1, 'alpha']
>> space.insert((None, "bravo"))
- [2, 'bravo']

You may read more about sequences here.

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

You can auto_increment on a primary key only. There is no way to auto increment other field in a tuple.

See here: https://tarantool.org/en/doc/1.6/book/box/box_space.html#box-space-auto-increment

Vladimir Lebedev
  • 1,207
  • 1
  • 11
  • 25
  • I expect to auto increment just the primary key, i.e. the first field of the tuple – M.E. Dec 14 '17 at 23:05
  • I wrote a separate answer with code snippets explaining how to use `auto_increment()` (now deprecated) and more proper `sequences` to have your primary key auto incremented. – Vladimir Lebedev Dec 18 '17 at 08:42