Search code examples
pythondatabasedatetimetype-conversionvaticle-typeql

TypeQL Insert datetime


I am trying to insert elements in python into my TypeDB database from a CSV file. I have the following problem, when I try to insert a date in a field, I have the following error

typedb.common.exception.TypeDBClientException: [QRY01] Invalid Query Pattern: The class 'ValueConstraint.Constant.String' cannot be casted to 'ValueConstraint.Constant.DateTime'.

This is my code

def format_date_time(date_string):
    date_obj = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%f")
    return date_obj.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]

...

typeql_insert_query += ', has opening-date "'+format_date_time(account["opening-date"])+'";'

I tried changing the date format to:

  • yyyy-mm-dd
  • yyyy-mm-ddThh:mm
  • yyyy-mm-ddThh:mm:ss
  • yyyy-mm-ddThh:mm:ss.f
  • yyyy-mm-ddThh:mm:ss.ff
  • yyyy-mm-ddThh:mm:ss.fff

I also tried to put my date like this in my TypeQL insertion:

typeql_insert_query += ', has opening-date datetime("'+format_date_time(account["opening-date"])+'");'


Solution

  • Your problem isn't the format of the datetime value, but the fact that you wrap it in quotes, which makes it a string:

    typeql_insert_query += ', has opening-date "'+format_date_time(account["opening-date"])+'";'
                                             # ~                                             ~
    

    Removing the quotes around the formatted datetime should resolve the issue.