In_transaction not called?

I am following the code from the topic ‘Auto Incrementing Field’ but it looks like the code is not called.

I have added my code to the ServerModule (is that correct?):

Code Sample:

@tables.in_transaction
def get_next_value_in_sequence():
  row = app_tables.alarms.get()
  row['id'] += 1
  return row['id']

Or should I call get_next_value_in_sequence() ???

Defining a function doesn’t execute it, it just makes it available to call. You do need to call get_next_value_in_sequence every time you want a new id.

Right. In my case I want to insert some fields in the alarms table AND increment the id field, so I have to add that too…

Something like this should work (untested code to give the idea):

@tables.in_transaction
def get_next_value_in_sequence():
  results = app_tables.alarms.search(tables.order_by('id', ascending=False))
  oldrow = results[0]

  newrow = app_tables.alarms.add_row(id=oldrow[id]+1)
  return newrow

That would give you a new row added with an incremented id, and then you could update individual fields of that row as needed.

Note that the above doesn’t work if there are no rows in the table.

1 Like

I tried your example, but got some errors:
anvil.server.SerializationError: Cannot serialize arguments to function. Cannot serialize <class ‘builtin_function’> object at msg[‘args’][0] at /libanvil/anvil/_threaded_server.py, line 373 called from /libanvil/anvil/_threaded_server.py, line 381 called from /libanvil/anvil/_server.py, line 41 called from /libanvil/anvil/_server.py, line 71 called from [ServerModule1, line 28](javascript:void(0)) called from /libanvil/anvil/tables/init.py, line 90 called from [Form1, line 16](javascript:void(0))

The code is now:

@anvil.server.callable
@anvil.tables.in_transaction
#def get_next_value_in_sequence():
def insert(what):
  now=datetime.datetime.now()
  results = app_tables.alarms.search(tables.order_by('id', ascending=False))
  oldrow = results[0]

  newrow = app_tables.alarms.add_row(id=oldrow[id]+1)
  newrow.update(datim=now, message=what) 
  return newrow

Which line is line 28? What’s being passed to the insert function on line 16 of Form1? There’s not enough context to be able to tell what’s causing the error message.

For what it’s worth, I would not combine @anvil.server.callable and @anvil.tables.in_transaction. The transaction handling can generate some false positives, and the more that’s done in the function the more likely that seems to be. You ideally want your transaction functions to be very targeted utility functions used by other server funcions.
I’d split out the insert and get_next_value_in_sequence functions:

@tables.in_transaction
def get_next_value_in_sequence():
  results = app_tables.alarms.search(tables.order_by('id', ascending=False))
  oldrow = results[0]

  newrow = app_tables.alarms.add_row(id=oldrow[id]+1)
  return newrow

@anvil.server.callable
def insert(what):
  row = get_next_value_in_sequence()
  now=datetime.datetime.now()
  row.update(datim=now, message=what) 

1 Like

This is line 28:
newrow = app_tables.alarms.add_row(id=oldrow[id]+1)

This is the code of Form1:

from ._anvil_designer import Form1Template
from anvil import *
import anvil.server
import anvil.tables as tables
import anvil.tables.query as q
from anvil.tables import app_tables

class Form1(Form1Template):

  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    # Any code you write here will run when the form opens.
    #anvil.server.call('debug', 'started')
    anvil.server.call('insert', 'some more')
    #anvil.server.call('update', 'some more')

BTW, calling

@anvil.server.callable
@anvil.tables.in_transaction

is described in the help file: Anvil Docs | Transactions

Read this for a description of false positives in transactions (and the entire thread for various people’s experiences with best practices using transactions): Non obvious causes of a transaction conflict? - #22 You’ll find that the general advice is to keep your transaction functions tiny and not try to combine them with other things.

I started out doing what the docs showed, and ran into transaction conflicts even though I was the only user of the app.

Looks like I forgot the quotes around id. I did warn you it was untested code.

2 Likes

It is starting to work now, the id’s in the table had a value of None. Changed that to 0, 1, 2, etc and now new records get added.
Thanks for your assistance (I used you latest code)

1 Like