railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Error Starting Rails Admin: Unsupported field datatype: timestamptz in rails admin 3.1.2 #3615

Open carlosgab83 opened 1 year ago

carlosgab83 commented 1 year ago

Having a postgreSQL table with a timstamp with time zone field type produce:

Unsupported field datatype: timestamptz

error while rails_admin access the model, http://localhost:3000/client

Reproduction steps

Expected behavior It must accept this data type as previous versions does

Stack

Stack trace

rails_admin (3.1.2) lib/rails_admin/config/fields/types.rb:14:in `block in load'
rails_admin (3.1.2) lib/rails_admin/config/fields/types.rb:14:in `fetch'
rails_admin (3.1.2) lib/rails_admin/config/fields/types.rb:14:in `load'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:17:in `block in <module:Fields>'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:57:in `block (2 levels) in factory'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:57:in `each'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:57:in `detect'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:57:in `block in factory'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:52:in `each'
rails_admin (3.1.2) lib/rails_admin/config/fields.rb:52:in `factory'
rails_admin (3.1.2) lib/rails_admin/config/has_fields.rb:135:in `_fields'
rails_admin (3.1.2) lib/rails_admin/config/has_fields.rb:138:in `_fields'
rails_admin (3.1.2) lib/rails_admin/config/has_fields.rb:109:in `all_fields'
rails_admin (3.1.2) lib/rails_admin/config/has_fields.rb:84:in `fields'
rails_admin (3.1.2) app/controllers/rails_admin/main_controller.rb:127:in `get_collection'

Context

If i change the type of the field to "timestamp without time zone", it works fine, but i need the time zone.

I know this was supported because it worked with rails_admin 2 and rails 5.

Inspecting the error in the gem i can see there are not a :timestamptz key in the types registry, but :timestamp only.

rails_admin (3.1.2) lib/rails_admin/config/fields/types.rb:14

>> @@registry.keys
=> [:text, :action_text, :string, :enum, :active_record_enum, :file_upload, :active_storage, :belongs_to_association, :boolean, :bson_object_id, :composite_keys_belongs_to_association, :datetime, :date, :numeric, :decimal, :dragonfly, :paperclip, :carrierwave, :multiple_file_upload, :multiple_active_storage, :multiple_carrierwave, :float, :has_many_association, :has_and_belongs_to_many_association, :has_one_association, :integer, :password, :polymorphic_association, :hidden, :serialized, :shrine, :time, :timestamp, :color, :simple_mde, :ck_editor, :code_mirror, :wysihtml5, :froala, :json, :jsonb, :inet, :uuid, :citext]

PostgresSQL table definition:

CREATE TABLE public.clients (
    id integer NOT NULL,
    name character varying(100) NOT NULL,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone,
    slug character varying(100) NOT NULL
);

Model definition:

app/models/client.rb

class Client < ApplicationRecord
end
shamangeorge commented 1 year ago

You could monkey patch it. In order to overcome this error for our project we added a timestamptz.rb file in the folder lib/rails_admin/config/fields/types/ and we then had to require it explicitly in lib/rails_admin/config/fields/types/all.rb

The contents of timestamptz.rb can be the same as timestamp.rb

carlosgab83 commented 1 year ago

Hi @shamangeorge, Thanks for your response. It works!.

Some considerations for someone facing the same issue:

It works as expected, even is applying the time zone correctly when save/update a record.

However i think this issue must be fixed in order to avoid this workaround