TableError: Column 'CreatedDate' is a date - cannot set it to a datetime

Hi all. I’m following the Talk Python Anvil training course, and I’ve hit a problem.

What I’m trying to do:
I have a form that allows a user to enter some details (their weight and resting heart rate, and the date that the measurement was taken) called AddMeasurementComponent. The component has a save button that invokes a server module to add the details the user has entered into a data table.

NB: this forum post appears to be the same issue, although I think I’m already doing the thing they did to resolve it.

What I’ve tried and what’s not working:
As far as I can tell, my code is the same as that of the course (samples below).

When the function to save the data runs, I get:

anvil.tables.TableError: Column 'CreatedDate' is a date - cannot set it to a datetime

As the error suggests, the column in the data table is of the type date. The error seems to be saying that my function is passing a datetime object. The full code is below, but the line where I set the date is self.date = self.date_of_measurement.date.

self.date_of_measurement is a standard date picker component from the Anvil toolbox.

Note that the debug print statement print(type(self.date)) returns <class 'datetime.date'>

Code Sample:
This code executes when the save button is clicked:

  def button_save_click(self, **event_args):
    self.label_error_msg.visible = False
    
    error = self.sync_data()
    if error:
      self.label_error_msg.text = error
      self.label_error_msg.visible = True
      return
      print(type(self.date))
#     print("Would have saved the measurement: {} {} {}".format(self.weight, self.rate, 
#                                                               self.date))
    
    anvil.server.call('add_measurement',self.date, self.weight, self.rate)
    navigation.go_home()

The code that sets the date (it’s in the try..except block):

 def sync_data(self):
    if not self.text_box_rate.text:
      return "Heart rate is required."
    
    if not self.text_box_weight.text:
      return "Weight is required."
    
    if not self.date_of_measurement.date:
      return "A measurement date is required."
    
    try:
      self.weight = int(self.text_box_weight.text)
      self.rate = int(self.text_box_rate.text)
      self.date = self.date_of_measurement.date
    except TypeError as te:
      return "Invalid Format: Could not convert data"
    except Exception as x:
      return "Unknown error: {}".format(x)
    
    return None

Clone link:
clone link

In your server function add_measurement you do:

created_date = datetime.datetime.now()

And then pass that variable to a column that is expecting a date. You’re passing a datetime into a date field.

Either change the field to be a datetime, or change the line to get the current date:

created_date = datetime.date.today()
2 Likes

@jshaffstall thank you so much much. Now you say it, it sounds obvious!

I’m not at my computer but I’ll be checking that ad soon as I can. Thank you.

1 Like

It turns out I’m an idiot :smiley:

Having had another look, my code was identical to that from the course. 100%. Even the created_date = datetime.date.today() line highlighted by @jshaffstall was the same as in the course code as mine. So how come theirs worked and mine didn’t?

Guess who missed that Michael Kennedy had created a DateTime column in the table, not a Date one? Oops. :smiley:

Once I fixed that, all was well.

4 Likes