Creating my first Transaction

Yes, that was the last row on my reply :slight_smile:

1 Like

ahh so sorry, my mind is a little blown today as am suffering with a migraine! I should probably walk away for the evening, but I really want to get this part done.

I’ve now removed PolicyNumber from all the dicts, and have this error

anvil.tables.TableError: Column 'PolicyNumber' is a Row from 'policies' table - cannot set it to a number

I set the tables up with the PolicyNumber Row as a linked row back to the policies table, should I just have created the same fieldname in all the tables I want to display that reference in and not linked it?

I assumed that PolicyNumber was a number… if it is not, then I need more information.

Looking at what I see, I’m going to guess that PolicyNumber is a column linked to the policies table on travellers and on mytrips, while it’s just a number in policies.

If that’s the case, then perhaps this is the solution:

def submit_policy(dict_travellers, dict_policies, dict_users, dict_mytrips):
    row = app_tables.counter.get()
    row['value'] += 1
    pn = row['value']

    # create new policy row with the new policy number
    row_policy = app_tables.policies.add_row(PolicyNumber=pn, **dict_policies)

    # use the row just created on the travellers and mytrips tables
    app_tables.travellers.add_row(PolicyNumber=row_policy, **dict_travellers)
    app_tables.mytrips.add_row(PolicyNumber=row_policy, **dict_mytrips)

    app_tables.users.get_current_user(**dict_users)
2 Likes

The submit_policy is producing 1 record, for each traveller, when I want it to produce

  • 1 policy record - same policy number
  • 1, trips record - same policy number
  • X traveller records = 1 for each traveller - all attached the same policy number
  • 1 x Update Current user record.

I’ve been struggling since yesterday to solve this, can anyone help I dont have long to solve this and I am getting nowhere :(.

If I move the indent on the dict_travellers, I only get 1 traveller record, not one for each traveller showing in the dict.

{'TravellerFirstname': 'Jon', 'TravellerSurname': 'Upton', 'TravellerDateofBirth': datetime.date(2000, 5, 21), 'User': <LiveObject: anvil.tables.Row>}

I created the same dict_travellers on an earlier click event and it showed correct at 2 records. So I think its definitely the indent thats wrong, but Anvils Editor shows an error if I move it in/out.

{'TravellerFirstname': 'Denise', 'TravellerSurname': 'Field', 'TravellerDateofBirth': datetime.date(2000, 5, 20), 'User': <LiveObject: anvil.tables.Row>}

{'TravellerFirstname': 'Jon', 'TravellerSurname': 'Upton', 'TravellerDateofBirth': datetime.date(2000, 5, 21), 'User': <LiveObject: anvil.tables.Row>}
  def button_complete_policy_click(self, **event_args):
      """This method is called when the button is clicked"""
      dict_policies = {
                "PolicyStartDate": self.trip_start_date,
                "PolicyEndDate": self.trip_end_date,
                "PolicyPremium": self.totalpremium,
                "PolicyIPT": self.totalipt,
                "User": anvil.users.get_user(),
                "PolicyTravellerCount": self.trav,
                "Region": self.location_region,
                "Country": self.location,
                "Days": self.days,
                "Baggage": self.finalbaggage,
                "SingleArticle": self.finalarticle,
                "Cancellation": self.finalcancellation,
                "PolicyDocument": "tba",
                "CreatedDate": date.today(),}
          # Add Trips
      dict_mytrips = {
                "TripName": self.tripname,
                "TripStartDate": self.trip_start_date,
                "TripEndDate": self.trip_end_date,
                "User": anvil.users.get_user(),
                "HolidayType": self.holidaytype,
                "Country": self.location,
                "City": self.location_city,
                "TripStatus": "Active",}
          # Create Dict for Update of User Row
      dict_users = {
                "UserAddress1": self.address1,
                "UserTown": self.town,
                "UserCounty": self.county,
                "UserPostcode": self.postcode,
                "UserMobile": self.mobile,
                "UserLandline": self.landline,}
      
      for traveller in self.cp2.get_components(): 
            dict_travellers = {
                "TravellerFirstname": traveller.traveller_firstname.text.title(),
                "TravellerSurname": traveller.traveller_surname.text.title(),
                "TravellerDateofBirth": traveller.travellerform_dob.date,
                "User": anvil.users.get_user(),
              }
      print(dict_travellers)  
      print(dict_policies)
      print(dict_users)
      print(dict_mytrips)
      anvil.server.call(
      'submit_policy',
        dict_policies,
        dict_users,
        dict_mytrips,
        dict_travellers,
    )

The other part that isnt working is on the server side, its producing an error on get_current_user:
app_tables.users.get_current_user(**dict_users)

I’ve been working more on this, and I have almost got it working on the client side, but to do this have had to make the tables writeable, have done this temporarily so I can at least get it running.

I am however, still stuck on updating the users table. I need to get the current logged in users record and then do this.


    for user in app_tables.users(anvil.users.get_user()): 
         
         dict_users = {
            "UserAddress1": self.address1,
            "UserTown": self.town,
            "UserCounty": self.county,
            "UserPostcode": self.postcode,
            "UserMobile": self.mobile,
            "UserLandline": self.landline,}
         print(dict_users)
         row.update(**dict_users)

But I am getting this error

TypeError: 'LiveObjectProxy' object is not callable

I know doing everything on the client side is not ideal, but I just want to get it working, any help would be much appreciated

Let’s break this down:

  • anvil is a module that contains lots of stuff, including other modules

  • anvil.users is a module

  • anvil.users.get_user is a function

  • anvil.users.get_user() is the execution of that function, and returns the currently logged in user

  • app_tables is a module

  • app_tables.users is a table

  • app_tables.users() is the execution of a table, which makes no sense

If all of the above is clear, then it’s clear that this:
app_tables.users(anvil.users.get_user())
is the execution of a table passing the current user as argument, and makes no sense.

The error message is telling you that you tried to call (execute) a LiveObjectProxy, which is the users table that you are trying to execute.


At this point the question is: what are you trying to do?

1 Like

Sorry about this, what I was trying to do is this:-

  1. Get the row on app_table.users that matches the current logged in user.
  2. Update that row with the results in dict_users. *
  • I have created on the users table all of those fields

Also, your code ends with row.update(**dict_users), but row is not defined.

current_user = anvil.users.get_user()

current_user.update(**dict_users)


PS
This long thread has drifted away and by now it has nothing to do with the subject. Creating new questions with the correct subject would make it easier for you to get an answer and for future users to learn from it.

1 Like

Thank you so much for your help. Yes I guess, in reality it has moved away and I really wasnt sure whether to create a new post, but then for me, I am still trying to update the same records in the database, so I wasnt sure what to do.

I am going to mark your post as the solution, since finally it all works :slight_smile: so thank you once again.

1 Like