@@ -324,6 +324,8 @@ def __setitem__(self, column, value):
324
324
------
325
325
QiitaDBColumnError
326
326
If the column does not exist in the table
327
+ ValueError
328
+ If the value type does not match the one in the DB
327
329
"""
328
330
conn_handler = SQLConnectionHandler ()
329
331
@@ -347,42 +349,42 @@ def __setitem__(self, column, value):
347
349
sql , (self ._table , column ))[0 ]
348
350
349
351
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 :
353
352
sql = """UPDATE qiita.{0}
354
353
SET {1}=%s
355
354
WHERE sample_id=%s""" .format (self ._dynamic_table ,
356
355
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
375
356
elif exists_required :
376
357
# here is not required the type check as the required fields have
377
358
# an explicit type check
378
359
sql = """UPDATE qiita.{0}
379
360
SET {1}=%s
380
361
WHERE sample_id=%s""" .format (self ._table , column )
381
- conn_handler .execute (sql , (value , self ._id ))
382
362
else :
383
363
raise QiitaDBColumnError ("Column %s does not exist in %s" %
384
364
(column , self ._dynamic_table ))
385
365
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
+
386
388
def __delitem__ (self , key ):
387
389
r"""Removes the sample with sample id `key` from the database
388
390
0 commit comments