Skip to content

remove psycopg2 dependency and change IntegrityError to Django's Inte…#440

Open
RossLYoung wants to merge 9 commits intomysociety:masterfrom
RossLYoung:psycopg3
Open

remove psycopg2 dependency and change IntegrityError to Django's Inte…#440
RossLYoung wants to merge 9 commits intomysociety:masterfrom
RossLYoung:psycopg3

Conversation

@RossLYoung
Copy link
Copy Markdown
Contributor

No description provided.

…parameter to an IN (...) clause — e.g., ('SML',) becomes ('SML') in SQL. psycopg3 removed this magic and treats tuples/values literally, causing the IN '(SML)' syntax error.

Fix: Changed IN %s (tuple) to = ANY(%s) (array), passing list(types) instead of tuple(types). psycopg3 natively adapts Python lists to PostgreSQL arrays, so = ANY(%s) with ['SML'] generates valid SQL: = ANY(ARRAY['SML']), which is semantically equivalent to IN ('SML').
…r_version, which is psycopg2-specific. In psycopg3 the version lives at conn.info.server_version, so this raises AttributeError, which bubbles up through filter_by_area and gets caught by the bare except: in example_postcode_for_area, silently setting pc = None.

Django's PostgreSQL backend exposes connection.pg_version (works with both psycopg2 and psycopg3):
1.
IN %s with tuple → = ANY(%s) with list (psycopg3 doesn't auto-adapt tuples for IN)
2.
connection.cursor().connection.server_version → connection.pg_version (psycopg3 uses conn.info.server_version internally)
3.
location::bytea → location (psycopg2 returns bytea as memoryview, psycopg3 as bytes; GEOSGeometry handles memoryview but fails on raw binary bytes)
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.07%. Comparing base (73a438f) to head (333b3dd).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #440      +/-   ##
==========================================
+ Coverage   74.20%   75.07%   +0.87%     
==========================================
  Files          47       48       +1     
  Lines        2822     2857      +35     
==========================================
+ Hits         2094     2145      +51     
+ Misses        728      712      -16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant