Devise inet fields may be treated as Field::String in Administrate, which results in SQL errors when searching
I'm seeing an ActiveRecord::StatementInvalid error when searching over my Users in Administrate.
Postgres gives the following error:
PG::UndefinedFunction: ERROR: function lower(inet) does not exist
The SQL query that's erroring looks like:
SELECT "users".* FROM "users" WHERE (lower(email) LIKE '%george%' OR lower(current_sign_in_ip) LIKE '%george%' ... ) LIMIT 20 OFFSET 0
As far as I can tell, this is because the current_sign_in_ip attribute (managed by Devise) is treated as a String in Administrate:
current_sign_in_ip: Field::String,
whereas it's actually an inet in Postgres:
t.inet "current_sign_in_ip"
so attempting to use lower on it fails.
This isn't an urgent issue-- simply deleting the offending attributes from ATTRIBUTE_TYPES in my user_dashboard.rb file allows me to search just fine. The only downside is that I can't see the current_sign_in_ip attribute in Administrate, which really doesn't matter. But I thought it was worth reporting.
you could make a custom field type for inet fields.. (I think)
@maxkwallace - is inet supported in other databases, or just postgres?
@gpinkham is right - you could create a class:
class InetField < Administrate::Field::String
def self.searchable?
false
end
end
with the partials to match.
Yup, that would definitely work. My main concern when opening this issue was not finding a solution as much as making sure Administrate doesn't appear to have bugs when people first set it up (hurts their impression of the gem, bad UX). So it may be worth adding a check for inet fields and excluding them from ATTRIBUTE_TYPES by default.
After checking:
- https://community.oracle.com/thread/290732
- https://msdn.microsoft.com/en-us/library/ms187752.aspx
- https://docs.mongodb.org/v3.0/reference/bson-types/
- http://stackoverflow.com/questions/5133580/which-mysql-datatype-for-an-ip-address
It looks like none of Oracle, SQL Server, Mongo or MySQL support a native inet type, so I assume Postgres is alone in this regard.
@maxkwallace you're right. At the moment, Administrate uses the String field type whenever it doesn't recognize the type of a database column. We can change it up so these unrecognized columns return searchable? => false.