ActiveRecord::StatementInvalid - PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8"

ActiveRecord::StatementInvalid - PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8"

这个问题的现有解决方案没有帮助。

Rails: 5.2 ruby : 2.4.1 PG: 0.18

This is the query from the rails log: 
INSERT INTO "users" ("first_name") VALUES ('\xC3\r\x04\t\x03\x02qlF\xA4\.....')

first_name 值使用 active_record-pgcrypto gem

加密

first_nameusers table

中的 string 数据类型

在 pgAdmin 中复制粘贴查询效果很好。但是应用程序抛出错误。

pgcrypto install documentation中所述,加密数据应使用:binary列。否则 Rails 和 Postgres 会将其解释为 UTF-8 字符,并且某些字节组合不是有效的 UTF-8 字符。