Skip to content

Commit d69be3b

Browse files
committed
Addressing @squirrelo's comment
1 parent fa4273e commit d69be3b

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from future.utils import viewitems, viewvalues
4040
from os.path import join
4141
from functools import partial
42+
from collections import defaultdict
4243

4344
import pandas as pd
4445

@@ -393,14 +394,21 @@ def __setitem__(self, column, value):
393394
except QiitaDBExecutionError as e:
394395
# catching error so we can check if the error is due to different
395396
# column type or something else
396-
if "invalid input syntax for" in str(e):
397-
column_type = conn_handler.execute_fetchone(
398-
"""SELECT data_type
399-
FROM information_schema.columns
400-
WHERE column_name=%s AND table_schema='qiita'
401-
""", (column,))[0]
402-
value_type = type(value).__name__
403-
397+
type_lookup = defaultdict(lambda: 'varchar')
398+
type_lookup[int] = 'integer'
399+
type_lookup[float] = 'float8'
400+
type_lookup[str] = 'varchar'
401+
value_type = type_lookup[type(value)]
402+
403+
sql = """SELECT udt_name
404+
FROM information_schema.columns
405+
WHERE column_name = %s
406+
AND table_schema = 'qiita'
407+
AND (table_name = %s OR table_name = %s)"""
408+
column_type = conn_handler.execute_fetchone(
409+
sql, (column, self._table, self._dynamic_table))
410+
411+
if column_type != value_type:
404412
raise ValueError(
405413
'The new value being added to column: "{0}" is "{1}" '
406414
'(type: "{2}"). However, this column in the DB is of '
@@ -1148,22 +1156,31 @@ def update_category(self, category, samples_and_values):
11481156
except QiitaDBExecutionError as e:
11491157
# catching error so we can check if the error is due to different
11501158
# column type or something else
1151-
if "invalid input syntax for" in str(e):
1152-
column_type = conn_handler.execute_fetchone(
1153-
"""SELECT data_type
1154-
FROM information_schema.columns
1155-
WHERE column_name=%s AND table_schema='qiita'
1156-
""", (category,))[0]
1157-
value_types = set(type(value).__name__
1158-
for value in viewvalues(samples_and_values))
1159-
value_types_str = ', '.join(value_types)
1159+
type_lookup = defaultdict(lambda: 'varchar')
1160+
type_lookup[int] = 'integer'
1161+
type_lookup[float] = 'float8'
1162+
type_lookup[str] = 'varchar'
1163+
value_types = set(type_lookup[type(value)]
1164+
for value in viewvalues(samples_and_values))
1165+
1166+
sql = """SELECT udt_name
1167+
FROM information_schema.columns
1168+
WHERE column_name = %s
1169+
AND table_schema = 'qiita'
1170+
AND (table_name = %s OR table_name = %s)"""
1171+
column_type = conn_handler.execute_fetchone(
1172+
sql, (category, self._table, self._table_name(self._id)))
1173+
1174+
if any([column_type != vt for vt in value_types]):
11601175
value_str = ', '.join(
11611176
[v for value in viewvalues(samples_and_values)])
1177+
value_types_str = ', '.join(value_types)
11621178

11631179
raise ValueError(
11641180
'The new values being added to column: "%s" are "%s" '
1165-
'(type: "%s"). However, this column in the DB is of '
1166-
'type "%s". Please change the value in your updated '
1181+
'(types: "%s"). However, this column in the DB is of '
1182+
'type "%s". Please change the values in your updated '
11671183
'template or reprocess your template.'
11681184
% (category, value_str, value_types_str, column_type))
1185+
11691186
raise e

0 commit comments

Comments
 (0)