Skip to content

fix(gdb): inject soft delete condition into LEFT JOIN ON clause instead of WHERE - #4816

Open
waterWang wants to merge 1 commit into
gogf:masterfrom
waterWang:fix/left-join-soft-delete-condition
Open

fix(gdb): inject soft delete condition into LEFT JOIN ON clause instead of WHERE#4816
waterWang wants to merge 1 commit into
gogf:masterfrom
waterWang:fix/left-join-soft-delete-condition

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Summary

For LEFT JOIN queries, the soft delete condition (deleted_at = 0) was previously added to the WHERE clause, which turns the LEFT JOIN into an INNER JOIN — filtering out rows from the main table when the joined table has no matching row.

Change

This fix detects LEFT JOINs and injects the soft delete condition into the ON clause instead, preserving the LEFT JOIN semantics.

Before (broken)

LEFT JOIN xy_member member ON (member.id = t.author_id)
WHERE t.id = 463 AND t.deleted_at = 0 AND member.deleted_at = 0

After (fixed)

LEFT JOIN xy_member member ON (member.id = t.author_id AND member.deleted_at = 0)
WHERE t.id = 463 AND t.deleted_at = 0

What changed

  • database/gdb/gdb_model_soft_time.go: Modified GetDeleteCondition to detect LEFT JOINs and inject the soft delete condition into the ON clause instead of the WHERE clause
  • Added injectSoftDeleteIntoLeftJoinOnClause helper method

Testing

The fix has been verified against the existing test suite. The soft time feature tests in contrib/drivers/*/*_z_unit_feature_soft_time_test.go continue to pass.

Related

Closes #4754

…ad of WHERE

For LEFT JOIN queries, the soft delete condition (deleted_at = 0) was
previously added to the WHERE clause, which turns the LEFT JOIN into
an INNER JOIN — filtering out rows from the main table when the joined
table has no matching row.

This fix detects LEFT JOINs and injects the soft delete condition into
the ON clause instead, preserving the LEFT JOIN semantics.

For example, the generated SQL changes from:
  LEFT JOIN member ON (member.id = t.author_id)
  WHERE ... AND member.deleted_at = 0

To:
  LEFT JOIN member ON (member.id = t.author_id AND member.deleted_at = 0)
  WHERE ... (only base table condition)

Closes gogf#4754
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

关于left join 查询软删除数据表,删除条件应当在on中而不是全局

1 participant