svoop
svoop

Reputation: 3464

Silence "unknown OID 17227: failed to recognize type of 'geography'. It will be treated as String."

I'm using columns of type "geography" for some few "point within polygon" queries. They are too few and too simple to bundle a GIS gem, I handle it all on the SQL level.

However, every time Rails boots (rake tasks, console etc), the following warning is spit:

unknown OID 17227: failed to recognize type of 'geography'. It will be treated as String.

I'm fine with "geography" being treated as "String", but the warning triggers warning mails every time a cronjob executes any rake task.

Any idea how I can silence this warning?

Thanks for your hints!

Upvotes: 2

Views: 2359

Answers (4)

svoop
svoop

Reputation: 3464

Looking at the source of ActiveRecord, I can answer the question myself:

The warning is hardcoded and therefore can't be silenced by AR configuration. However, RUBYOPT=-W0 gets rid of warnings altogether. This is a big chopper of course, but since I'm still getting those warnings in local development, I can live with a totally warning-less production system.


Update 2021:

After having updated the app to Rails 6.1, I got rid of this ugly hack and aliased the type properly in config/initializers/types.rb:

# (other custom types here)

# This application uses the PostGIS extension without the corresponding postgis
# adapter gem and therefore the "geography" type is unknown to ActiveRecord which
# causes a warning:
#
# unknown OID: failed to recognize type of 'geography'. It will be treated as String.
#
# The following silences this warning by explicitly aliasing "geography" to "text".
module PostgresGeographyExtension
  def load_additional_types(oids=nil)
    type_map.alias_type 'geography', 'text'
    super
  end
end
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend PostgresGeographyExtension

Upvotes: 0

Allison
Allison

Reputation: 2343

Please consider that warnings exist for a reason, and there are very often repercussions for ignoring them that may only become apparent with time (and be all the more difficult to diagnose when you've intentionally blinded yourself to them and put them out of your mind).

You were using the wrong adapter for your database. If you're getting that error, you need to change the value of adapter in your config/database.yml to be postgis + have the postgis adapter installed.

Upvotes: 0

Marrs
Marrs

Reputation: 716

Looking at this postgresql statement executed in a rails console:

ActiveRecord::Base.connection.exec_query(
  <<-SQL.squish
    SELECT websearch_to_tsquery('simple', 'james m') AS some_named_value
  SQL
).to_a.first['some_named_value']

will show this error in the console when it runs:

unknown OID 3615: failed to recognize type of 'some_named_value'. It will be treated as String.

Casting the datatype of tsquery to the datatype of text removes the error in this case (it should only matter for the last SELECT that reads out in the exec_query). I assume you do something similar!

ActiveRecord::Base.connection.exec_query(
  <<-SQL.squish
    SELECT websearch_to_tsquery('simple', 'james m')::text AS some_named_value
  SQL
).to_a.first['some_named_value']

Upvotes: 1

Ayudh
Ayudh

Reputation: 1763

Since this is the first question/answer that appears given that the title is the error, I would recommend following this answer to fix it: What is the source of "unknown OID" errors in Rails?

Upvotes: 2

Related Questions