Skip to content

Fix bulk_update raising syntax error for ForeignKey fields#2126

Open
Br1an67 wants to merge 2 commits intotortoise:developfrom
Br1an67:fix/bulk-update-fk
Open

Fix bulk_update raising syntax error for ForeignKey fields#2126
Br1an67 wants to merge 2 commits intotortoise:developfrom
Br1an67:fix/bulk-update-fk

Conversation

@Br1an67
Copy link
Contributor

@Br1an67 Br1an67 commented Mar 1, 2026

Description

When bulk_update was called with a ForeignKey field name (e.g., fields=['post']), the generated SQL used the Python object repr (e.g., <Post>) instead of the FK column value, causing a syntax error like near "<": syntax error.

The fix resolves FK fields to their source_field column name and extracts the related object's PK value, matching the behavior of the regular UpdateQuery.

Motivation and Context

Fixes #1335

Using fields=['post_id'] worked as a workaround, but fields=['post'] (the FK field name) should also work, as it does for regular .update() queries.

How Has This Been Tested?

Added test_bulk_update_foreign_key that:

  1. Creates events linked to tournament1
  2. Reassigns them to tournament2
  3. Calls bulk_update(events, fields=['tournament'])
  4. Verifies the FK was correctly updated in the database

All 8 existing bulk_update tests continue to pass.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 3, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing Br1an67:fix/bulk-update-fk (15da693) with develop (f61f641)

Open in CodSpeed

@abondar
Copy link
Member

abondar commented Mar 3, 2026

@Br1an67 Also check linter here, pls

@Br1an67 Br1an67 force-pushed the fix/bulk-update-fk branch 3 times, most recently from 12436d0 to bc8c9f8 Compare March 3, 2026 15:53
When bulk_update was called with a ForeignKey field name (e.g.,
fields=['post']), the generated SQL used the Python object repr
instead of the FK column value, causing a syntax error. Now FK
fields are properly resolved to their source_field column name
and the related object's PK value is used in the SQL.
@Br1an67 Br1an67 force-pushed the fix/bulk-update-fk branch from bc8c9f8 to 030a778 Compare March 3, 2026 16:04
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.

bulk_update raises syntax error when fields contains a foreign key

2 participants