Hi there,
Anvil deals with databases a little differently than you might be used to. You don’t have to think about the unique identifier of a row (I mean, you can get it, with row.get_id()
, but you don’t need to). Instead, in Anvil you deal with row objects. They’re a bit like dictionaries - you can do:
print row['name']
But you can also do this to update a column:
row['name'] = "Athelene"
# Also works: row.update(name="Athelene")
When you get (or set) the value of a link column, you don’t get the unique ID of a row - you get the row object itself. So, if you had the table structure you described in your previous post, you could do:
for row in app_tables.coa.search():
print "Category: %s; Year: %d" % (row['category_name'], row['fy']['fyear'])
Note that because the “fy” column in your coa
table is a link to another table, when we do row['fy']
we get row object back – which means we can immediately look up the 'fyear'
column in that row.
Anyway, all of this is to say that, if you want to select a table row from a DropDown, 95% of the time this is what you want:
self.drop_down_1.items = [(str(row['fyear']), row) for row in app_tables.fy.search()]
That will make a DropDown with an item for every row in your fy
table. Each item is labelled with the year (the stringified value of the 'fyear'
column), but when you select, an item, the DropDown’s selected_value
property will refer to the row object itself.
This means you can then do:
print "Adding new record to FY %d" % self.drop_down_1.selected_value['fyear']
app_tables.coa.add_row(category='whatever',
category_type='X',
fy=self.drop_down_1.selected_value)
See that self.drop_down_1.selected_value
is a row object from the fy
table (so we can do things like looking up columns on it). You use that row object to fill in the 'fy'
column in the new coa
row - that’s how you create a link between tables in Anvil.
I hope that clarifies how table linking works in Anvil!