Handle OID types in queries
masterBecause Postgrex uses the PostgreSQL binary protocol, OID types (like regclass) are expected as integers rather than strings. If you pass a string to a parameter expecting an OID, the query will fail.
You have three ways to handle this:
- Explicit Cast: Cast the parameter in the SQL string (e.g.,
$1::text::regclass). - Pre-determine OID: Query for the OID once, store it, and use the integer in subsequent queries (most efficient).
- Pass Integer: Pass the integer OID directly.
# Option 1: Explicit cast (works with binary protocol)
query("select nextval($1::text::regclass)", ["some_sequence"])
# Option 2: Determine OID once and reuse (most efficient)
%{rows: [{sequence_oid}]} = query("select $1::text::regclass", ["some_sequence"])
query("select nextval($1)", [sequence_oid])