|
39 | 39 | from future.utils import viewitems, viewvalues
|
40 | 40 | from os.path import join
|
41 | 41 | from functools import partial
|
| 42 | +from collections import defaultdict |
42 | 43 |
|
43 | 44 | import pandas as pd
|
44 | 45 |
|
@@ -393,14 +394,21 @@ def __setitem__(self, column, value):
|
393 | 394 | except QiitaDBExecutionError as e:
|
394 | 395 | # catching error so we can check if the error is due to different
|
395 | 396 | # 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: |
404 | 412 | raise ValueError(
|
405 | 413 | 'The new value being added to column: "{0}" is "{1}" '
|
406 | 414 | '(type: "{2}"). However, this column in the DB is of '
|
@@ -1148,22 +1156,31 @@ def update_category(self, category, samples_and_values):
|
1148 | 1156 | except QiitaDBExecutionError as e:
|
1149 | 1157 | # catching error so we can check if the error is due to different
|
1150 | 1158 | # 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]): |
1160 | 1175 | value_str = ', '.join(
|
1161 | 1176 | [v for value in viewvalues(samples_and_values)])
|
| 1177 | + value_types_str = ', '.join(value_types) |
1162 | 1178 |
|
1163 | 1179 | raise ValueError(
|
1164 | 1180 | '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 ' |
1167 | 1183 | 'template or reprocess your template.'
|
1168 | 1184 | % (category, value_str, value_types_str, column_type))
|
| 1185 | + |
1169 | 1186 | raise e
|
0 commit comments