diff --git a/mylib/pgsql.py b/mylib/pgsql.py index 9b7313f..606fc9e 100644 --- a/mylib/pgsql.py +++ b/mylib/pgsql.py @@ -225,12 +225,20 @@ class PgDB: ) if isinstance(where_clauses, dict): - cls._combine_params(params, where_clauses) + sql_where_clauses = [] + for field, value in where_clauses.items(): + param = field + if field in params: + idx = 1 + while param in params: + param = '%s_%d' % (field, idx) + idx += 1 + cls._combine_params(params, {param: value}) + sql_where_clauses.append( + '"{field}" = %({param})s'.format(field=field, param=param) + ) return ( - (" %s " % where_op).join([ - '"{0}" = %({0})s'.format(key) - for key in where_clauses - ]), + (" %s " % where_op).join(sql_where_clauses), params ) raise PgDBUnsupportedWHEREClauses(where_clauses) diff --git a/tests/test_pgsql.py b/tests/test_pgsql.py index 708739e..42b757f 100644 --- a/tests/test_pgsql.py +++ b/tests/test_pgsql.py @@ -212,6 +212,15 @@ def test_add_where_clauses_with_op(): dict() ) +def test_add_where_clauses_with_duplicated_field(): + sql="UPDATE table SET test1=%(test1)s" + params = dict(test1='new_value') + where_clauses = dict(test1='where_value') + assert PgDB._add_where_clauses(sql, params, where_clauses) == ( + sql + ' WHERE "test1" = %(test1_1)s', + dict(test1='new_value', test1_1='where_value') + ) + def test_insert(mocker, test_pgdb): values = dict(test1=1, test2=2) mocker.patch(