Triplicate rows from Model Class

With fewer typos this time (probably), here’s the code I’m running:

from anvil.tables import app_tables


class TodoItem(app_tables.todos.Row, buffered=True, attrs=True, client_writable=True):
    def _do_delete(self, from_client):
        super()._do_delete()

    def _do_update(self, updates, from_client):
        super()._do_update(updates, from_client)

    @classmethod
    def _do_create(cls, values, from_client):
        return super()._do_create(values, from_client)


test = TodoItem(task="test")
test.save()

test2 = TodoItem()
test2.task = "test2"
test2.save()

Why do I end up with 6 rows in the db table when I run this? Both test and test2 are in in there three times.

Clone link:

because you’re doing it in the module scope

this module gets imported on the server because it’s a portable class

run client code, import model module
save a draft on the client
execute the code on the server to save the draft
run server code
import client module to load the model
save a draft on the server

And you’re doing two separate saves in the module
So you end up with 2 saves from the client
then 2 saves on the server from saving the first draft
and 2 more saves on the server from saving the second draft

:tada:

2 Likes

Gotcha. No wonder I can’t reproduce it elsewhere.

A slightly too minimal MWE!

Thanks again.