administrate icon indicating copy to clipboard operation
administrate copied to clipboard

Devise inet fields may be treated as Field::String in Administrate, which results in SQL errors when searching

Open marisawallace opened this issue 11 years ago • 4 comments

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.

marisawallace avatar Nov 06 '15 16:11 marisawallace

you could make a custom field type for inet fields.. (I think)

gpinkham avatar Nov 06 '15 18:11 gpinkham

@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.

c-lliope avatar Nov 06 '15 18:11 c-lliope

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.

marisawallace avatar Nov 07 '15 10:11 marisawallace

@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.

c-lliope avatar Nov 07 '15 17:11 c-lliope