The N. wrote:
On Nov 5, 2:07 pm, Phillip K. [email protected]
wrote:
if you want to use a char instead of a character varying, you can
override native_database_types to include a :char definition.
Really? How do I override it? I’m interested. Sorry if I have many
trivial questions, but I’m really new to Rails and all this stuff of
the migrations.
Each database adapter has the ability to define the database types Rails
uses when building DDL. The method used is native_database_types, which
is, in the case of PostgreSQL, found in the PostgreSQL class of the
ConnectionAdapters module of the ActiveRecord module. Override it and
supply what you want. Here is what I use to add bigint support to AR:
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
in order to add or change the datatypes, this function
must be overriden. Be careful, then, to not remove anything.
That carries with it the warning that if Rails Core changes
this function, this override will do away with those changes!
def native_database_types
{
:primary_key => “serial primary key”,
:string => { :name => “character varying”, :limit => 255 },
:text => { :name => “text” },
:integer => { :name => “integer” },
:float => { :name => “float” },
:decimal => { :name => “decimal” },
:datetime => { :name => “timestamp” },
:timestamp => { :name => “timestamp” },
:time => { :name => “time” },
:date => { :name => “date” },
:binary => { :name => “bytea” },
:boolean => { :name => “boolean” },
:bigint => { :name => “int8” }
}
end
end
Put that in a file that gets loaded when the Rails environment gets
loaded, and you’ve overridden the method. In your case, if you want to
add char support, you might do something like:
def native_database_types
{
:primary_key => “serial primary key”,
:char => { :name => “character”, :limit => 255 },
:string => { :name => “character varying”, :limit => 255 },
:text => { :name => “text” },
…
}
end
and then in your migration
t.column :char, :my_column, :limit => 5
in Rails 2.x, you can shorten that to
t.string :my_column
but I haven’t figured out how to make that work with the additions I’ve
made to native_database_types. So I don’t know how to achieve
t.char :my_column
which means you still need to use the older way of t.column
Peace.