Skip to content

Commit

Permalink
fix: multiple join in SQLizer.bulk_update_from_dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
NightMarcher committed Feb 5, 2024
1 parent 75508c4 commit b274011
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ Generate sql and execute
```python
await AccountMgr.bulk_update_from_dicts(
[
{"id": 7, "active": False, "gender": GenderEnum.male, "extend": {"test": 1, "debug": 0}},
{"id": 15, "active": True, "gender": GenderEnum.unknown, "extend": {"test": 1, "debug": 0}}
{"id": 7, "active": False, "deleted": False, "gender": GenderEnum.male, "extend": {"test": 1, "debug": 0}},
{"id": 15, "active": True, "deleted": False, "gender": GenderEnum.unknown, "extend": {"test": 1, "debug": 0}}
],
join_fields=["id"],
join_fields=["id", "deleted"],
update_fields=["active", "gender"],
merge_fields=["extend"],
)
Expand All @@ -180,9 +180,9 @@ Generate sql and execute
JOIN (
SELECT * FROM (
VALUES
ROW(7, False, 1, '{"test": 1, "debug": 0}'),
ROW(15, True, 0, '{"test": 1, "debug": 0}')
) AS fly_table (id, active, gender, extend)
) tmp ON `account`.id=tmp.id
ROW(7, False, False, 1, '{"test": 1, "debug": 0}'),
ROW(15, False, True, 0, '{"test": 1, "debug": 0}')
) AS fly_table (id, deleted, active, gender, extend)
) tmp ON `account`.id=tmp.id AND `account`.deleted=tmp.deleted
SET `account`.active=tmp.active, `account`.gender=tmp.gender, `account`.extend=JSON_MERGE_PATCH(COALESCE(`account`.extend, '{}'), tmp.extend)
```
2 changes: 1 addition & 1 deletion fastapi_esql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
wrap_backticks,
)

__version__ = "0.0.12"
__version__ = "0.0.13"

__all__ = [
"QsParsingError",
Expand Down
2 changes: 1 addition & 1 deletion fastapi_esql/utils/sqlizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def bulk_update_from_dicts(
sql = f"""
UPDATE {wrap_backticks(table)}
JOIN ({SQLizer.build_fly_table(dicts, join_fields + update_fields + merge_fields, using_values, log_sql=False)}
) tmp ON {", ".join(joins)}
) tmp ON {" AND ".join(joins)}
SET {", ".join(updates)}
"""
logger.debug(sql)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fastapi-efficient-sql"
version = "0.0.12"
version = "0.0.13"
description = "Generate bulk DML SQL and execute them based on Tortoise ORM and mysql8.0+, and integrated with FastAPI."
authors = ["BryanLee <[email protected]>"]
keywords = ["sql", "fastapi", "tortoise-orm", "mysql8", "bulk-operation"]
Expand Down
14 changes: 7 additions & 7 deletions tests/test_sqlizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ def test_bulk_update_from_dicts(self):
sql = SQLizer.bulk_update_from_dicts(
self.table,
[
{"id": 7, "active": False, "gender": GenderEnum.male, "extend": {"test": 1, "debug": 0}},
{"id": 15, "active": True, "gender": GenderEnum.unknown, "extend": {"test": 1, "debug": 0}}
{"id": 7, "active": False, "deleted": False, "gender": GenderEnum.male, "extend": {"test": 1, "debug": 0}},
{"id": 15, "active": True, "deleted": False, "gender": GenderEnum.unknown, "extend": {"test": 1, "debug": 0}}
],
join_fields=["id"],
join_fields=["id", "deleted"],
update_fields=["active", "gender"],
merge_fields=["extend"],
)
Expand All @@ -381,10 +381,10 @@ def test_bulk_update_from_dicts(self):
JOIN (
SELECT * FROM (
VALUES
ROW(7, False, 1, '{"test": 1, "debug": 0}'),
ROW(15, True, 0, '{"test": 1, "debug": 0}')
) AS fly_table (id, active, gender, extend)
) tmp ON `account`.id=tmp.id
ROW(7, False, False, 1, '{"test": 1, "debug": 0}'),
ROW(15, False, True, 0, '{"test": 1, "debug": 0}')
) AS fly_table (id, deleted, active, gender, extend)
) tmp ON `account`.id=tmp.id AND `account`.deleted=tmp.deleted
SET `account`.active=tmp.active, `account`.gender=tmp.gender, `account`.extend=JSON_MERGE_PATCH(COALESCE(`account`.extend, '{}'), tmp.extend)
"""

Expand Down

0 comments on commit b274011

Please sign in to comment.