connection.select_value only returns strings in postgres with pg gem

心已入冬 提交于 2019-11-28 12:10:28

The short answer is no. The 'pg' driver intentionally provides as thin a layer as possible on top of the native 'libpq' driver. It doesn't do typecasting, as that's the responsibility of higher-level libraries that have some insight into the domain in which the results will be used. The rationale for this decision is documented on the PostgreSQL Wiki, and I'd be happy to discuss it with you further on the mailing list.

For those who land here looking for an activerecord attributes (Rails) specific answer: I've done some tests (https://gist.github.com/gamov/8fe38733012931eb3360) and discovered that:

RequestedItem.where(id: 1).select(*, 10 AS tq).first.tq.class

returns a String with Rails < 4 and a Fixnum with Rails >= 4.

The Postgres adapter will forward the types from the DB to the Persistence module so casting can be done transparently.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!