@@ -324,6 +324,8 @@ def __setitem__(self, column, value):
324324 ------
325325 QiitaDBColumnError
326326 If the column does not exist in the table
327+ ValueError
328+ If the value type does not match the one in the DB
327329 """
328330 conn_handler = SQLConnectionHandler ()
329331
@@ -347,42 +349,42 @@ def __setitem__(self, column, value):
347349 sql , (self ._table , column ))[0 ]
348350
349351 if exists_dynamic :
350- # catching error so we can check if the error is due to different
351- # column type or something else
352- try :
353352 sql = """UPDATE qiita.{0}
354353 SET {1}=%s
355354 WHERE sample_id=%s""" .format (self ._dynamic_table ,
356355 column )
357- conn_handler .execute (sql , (value , self ._id ))
358- except Exception as e :
359- column_type = conn_handler .execute_fetchone (
360- """SELECT data_type
361- FROM information_schema.columns
362- WHERE column_name=%s AND table_schema='qiita'
363- """ , (column ,))[0 ]
364- value_type = type (value ).__name__
365-
366- if column_type != value_type :
367- raise ValueError (
368- 'The new value being added to column: "{0}" is "{1}" '
369- '(type: "{2}"). However, this column in the DB is of '
370- 'type "{3}". Please change the value in your updated '
371- 'template or reprocess your sample template.' .format (
372- column , value , value_type , column_type ))
373- else :
374- raise e
375356 elif exists_required :
376357 # here is not required the type check as the required fields have
377358 # an explicit type check
378359 sql = """UPDATE qiita.{0}
379360 SET {1}=%s
380361 WHERE sample_id=%s""" .format (self ._table , column )
381- conn_handler .execute (sql , (value , self ._id ))
382362 else :
383363 raise QiitaDBColumnError ("Column %s does not exist in %s" %
384364 (column , self ._dynamic_table ))
385365
366+ try :
367+ conn_handler .execute (sql , (value , self ._id ))
368+ except Exception as e :
369+ # catching error so we can check if the error is due to different
370+ # column type or something else
371+ column_type = conn_handler .execute_fetchone (
372+ """SELECT data_type
373+ FROM information_schema.columns
374+ WHERE column_name=%s AND table_schema='qiita'
375+ """ , (column ,))[0 ]
376+ value_type = type (value ).__name__
377+
378+ if column_type != value_type :
379+ raise ValueError (
380+ 'The new value being added to column: "{0}" is "{1}" '
381+ '(type: "{2}"). However, this column in the DB is of '
382+ 'type "{3}". Please change the value in your updated '
383+ 'template or reprocess your sample template.' .format (
384+ column , value , value_type , column_type ))
385+ else :
386+ raise e
387+
386388 def __delitem__ (self , key ):
387389 r"""Removes the sample with sample id `key` from the database
388390
0 commit comments