Search code examples
python-2.7tarantool

Tarantool in Python - how to call auto_increment function


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')

Solution

  • 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.