Detect which row and field is causing the error

I can’t seem to figure out which row’s field is causing the issue.

Since the MySQL data was imported from the a LAMP stack code base, I want to suppress the errors and take whatever value exists in the dateTime / date field.

This following always prints id = 0

tbl_id = 0
try:
    inv = Aws_inventory.objects.all().order_by('-id')[:1000]
    for item in inv:
        tbl_id = item.id
        print(item.id)
except ValueError as e:
    if 'hour must be in 0..23' in str(e):
        print("Something went wrong here", tbl_id)
    else:
        raise e

Here the error is thrown before its even entering the try except block.

tbl_id = 0
inv = Aws_inventory.objects.all().order_by('-id')[:1000]
for item in inv:
    try:
        tbl_id = item.id
        print(item.id)
    except ValueError as e:
        if 'hour must be in 0..23' in str(e):
            print("Something went wrong here", tbl_id)
        else:
            raise e

So I’m assuming the error is being thrown by this line?

You haven’t identified what the error is that is being thrown.

It would be helpful if you posted the complete error with traceback.

The error thrown is “hour must be in 0…23” but it doesn’t print the first n rows (item.id) before throwing the error. It is throwing the error before the first print.

It would be useful to see the Traceback. But your code prevents that by catching the exception. Could you add after your print statement in the except clause: raise e?

Then you should now see the error with its Traceback. Post it back here if you need more help. :smiley:

my clean() method is empty - I tried to clean one of the fields here before. So ignore the clean() line.

In [1]: from server.models import Aws_inventory

In [2]: inv = Aws_inventory.objects.all().order_by('-id')[:1000]

In [3]:     for item in inv:
   ...:         item.clean()
   ...:         print(item)
   ...:
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 for item in inv:
      2     item.clean()
      3     print(item)

File D:\workspace\django\projectName\env\lib\site-packages\django\db\models\query.py:398, in QuerySet.__iter__(self)
    383 def __iter__(self):
    384     """
    385     The queryset iterator protocol uses three nested iterators in the
    386     default case:
   (...)
    396            - Responsible for turning the rows into model objects.
    397     """
--> 398     self._fetch_all()
    399     return iter(self._result_cache)

File D:\workspace\django\projectName\env\lib\site-packages\django\db\models\query.py:1881, in QuerySet._fetch_all(self)
   1879 def _fetch_all(self):
   1880     if self._result_cache is None:
-> 1881         self._result_cache = list(self._iterable_class(self))
   1882     if self._prefetch_related_lookups and not self._prefetch_done:
   1883         self._prefetch_related_objects()

File D:\workspace\django\projectName\env\lib\site-packages\django\db\models\query.py:91, in ModelIterable.__iter__(self)
     88 compiler = queryset.query.get_compiler(using=db)
     89 # Execute the query. This will also fill compiler.select, klass_info,
     90 # and annotations.
---> 91 results = compiler.execute_sql(
     92     chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size
     93 )
     94 select, klass_info, annotation_col_map = (
     95     compiler.select,
     96     compiler.klass_info,
     97     compiler.annotation_col_map,
     98 )
     99 model_cls = klass_info["model"]

File D:\workspace\django\projectName\env\lib\site-packages\django\db\models\sql\compiler.py:1562, in SQLCompiler.execute_sql(self, result_type, chunked_fetch, chunk_size)
   1560     cursor = self.connection.cursor()
   1561 try:
-> 1562     cursor.execute(sql, params)
   1563 except Exception:
   1564     # Might fail for server-side cursors (e.g. connection closed)
   1565     cursor.close()

File D:\workspace\django\projectName\env\lib\site-packages\django\db\backends\utils.py:102, in CursorDebugWrapper.execute(self, sql, params)
    100 def execute(self, sql, params=None):
    101     with self.debug_sql(sql, params, use_last_executed_query=True):
--> 102         return super().execute(sql, params)

File D:\workspace\django\projectName\env\lib\site-packages\django\db\backends\utils.py:67, in CursorWrapper.execute(self, sql, params)
     66 def execute(self, sql, params=None):
---> 67     return self._execute_with_wrappers(
     68         sql, params, many=False, executor=self._execute
     69     )

File D:\workspace\django\projectName\env\lib\site-packages\django\db\backends\utils.py:80, in CursorWrapper._execute_with_wrappers(self, sql, params, many, executor)
     78 for wrapper in reversed(self.db.execute_wrappers):
     79     executor = functools.partial(wrapper, executor)
---> 80 return executor(sql, params, many, context)

File D:\workspace\django\projectName\env\lib\site-packages\django\db\backends\utils.py:89, in CursorWrapper._execute(self, sql, params, *ignored_wrapper_args)
     87     return self.cursor.execute(sql)
     88 else:
---> 89     return self.cursor.execute(sql, params)

File D:\workspace\django\projectName\env\lib\site-packages\django\db\backends\mysql\base.py:75, in CursorWrapper.execute(self, query, args)
     72 def execute(self, query, args=None):
     73     try:
     74         # args is None means no string interpolation
---> 75         return self.cursor.execute(query, args)
     76     except Database.OperationalError as e:
     77         # Map some error codes to IntegrityError, since they seem to be
     78         # misclassified and Django would prefer the more logical place.
     79         if e.args[0] in self.codes_for_integrityerror:

File D:\workspace\django\projectName\env\lib\site-packages\MySQLdb\cursors.py:206, in BaseCursor.execute(self, query, args)
    203         raise ProgrammingError(str(m))
    205 assert isinstance(query, (bytes, bytearray))
--> 206 res = self._query(query)
    207 return res

File D:\workspace\django\projectName\env\lib\site-packages\MySQLdb\cursors.py:321, in BaseCursor._query(self, q)
    319 db.query(q)
    320 self._do_get_result(db)
--> 321 self._post_get_result()
    322 self._executed = q
    323 return self.rowcount

File D:\workspace\django\projectName\env\lib\site-packages\MySQLdb\cursors.py:355, in CursorStoreResultMixIn._post_get_result(self)
    354 def _post_get_result(self):
--> 355     self._rows = self._fetch_row(0)
    356     self._result = None

File D:\workspace\django\projectName\env\lib\site-packages\MySQLdb\cursors.py:328, in BaseCursor._fetch_row(self, size)
    326 if not self._result:
    327     return ()
--> 328 return self._result.fetch_row(size, self._fetch_type)

File D:\workspace\django\projectName\env\lib\site-packages\django\db\backends\utils.py:206, in typecast_time(s)
    204 else:
    205     microseconds = "0"
--> 206 return datetime.time(
    207     int(hour), int(minutes), int(seconds), int((microseconds + "000000")[:6])
    208 )

ValueError: hour must be in 0..23

[quote=“anjanesh, post:5, topic:34658”]

  1. What is your model code for Aws_inventory?
  2. You could use Django’s extra() method and filter out the invalid dates:
tbl_id = 0
try:
    inv = Aws_inventory.objects.extra(
        where=["STR_TO_DATE(date_column, '%Y-%m-%d %H:%i:%s') IS NOT NULL"]
    ).order_by('-id')[:1000]
    for item in inv:
        try:
            tbl_id = item.id
            print(item.id)
        except ValueError as e:
            if 'hour must be in 0..23' in str(e):
                print("Something went wrong here", tbl_id)
            else:
                raise e
except Exception as e:
    print(f"An error occurred: {e}")