Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using binary data #54

Open
bryanp opened this issue Aug 2, 2023 · 2 comments
Open

Using binary data #54

bryanp opened this issue Aug 2, 2023 · 2 comments

Comments

@bryanp
Copy link

bryanp commented Aug 2, 2023

I'm using mini_sql to query a bytea column in postgres, but I'm having trouble getting the query to run. Here's the error:

PG::CharacterNotInRepertoire:
  ERROR:  invalid byte sequence for encoding "UTF8": 0xe1 0x46 0xe0

Using pg directly, I can do something like this:

sql = <<~SQL
  select from secrets where key = $1
SQL

db.exec_params(sql, [
  {value: SecureRandom.bytes(32), format: 1},
])

However I'm not too sure the best way to go about solving this using mini_sql. Note that I can get the same PG::CharacterNotInRepertoire error using pg directly with the following approach:

sql = <<~SQL
  select from secrets where key = '#{SecureRandom.bytes(32)}'
SQL

db.exec(sql)

Curious if there's a way around this issue or if additional support is needed.

@bryanp
Copy link
Author

bryanp commented Aug 2, 2023

FWIW I did find a workaround:

where("key = :key", key: PG::TextEncoder::Bytea.new.encode(SecureRandom.bytes(32)))

Curious if you think this should have better built-in support. If so, I'd be happy to brainstorm potential solutions.

@SamSaffron
Copy link
Member

its tricky, how would we easily know something is binary data? for starters though can you add a note to the readme?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants