Florian Koch
Florian Koch

Reputation: 1502

List of ActiveRecord DB datatypes as symbols/strings

This is a really short question: Is there a list of all datatypes I can use for columns in a mysql database with ActiveRecord?

I know the datatypes and that they are all listed in the docs, but I want to know if there is already a collection of the types I could use to populate an html <select>. I'd like to avoid creating an own list for this if it already exists, but I can't seem to find one.

Upvotes: 1

Views: 386

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14900

You can use this command (run on an app using PG)

ActiveRecord::Base.connection.native_database_types.keys

# [:primary_key, :string, :text, :integer, :float, :decimal,
  :datetime, :time, :date, :daterange, :numrange, :tsrange,
  :tstzrange, :int4range, :int8range, :binary, :boolean, :xml, 
  :tsvector, :hstore, :inet, :cidr, :macaddr, :uuid, :json, :jsonb, 
  :ltree, :citext, :point, :line, :lseg, :box, :path, :polygon, 
  :circle, :bit, :bit_varying, :money]

Postgres:

You can also browse the source code and get this massive array.

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L73-L114

NATIVE_DATABASE_TYPES = {
    primary_key: "bigserial primary key",
    string:      { name: "character varying" },
    text:        { name: "text" },
    integer:     { name: "integer" },
    float:       { name: "float" },
    decimal:     { name: "decimal" },
    datetime:    { name: "timestamp" },
    time:        { name: "time" },
    date:        { name: "date" },
    daterange:   { name: "daterange" },
    numrange:    { name: "numrange" },
    tsrange:     { name: "tsrange" },
    tstzrange:   { name: "tstzrange" },
    int4range:   { name: "int4range" },
    int8range:   { name: "int8range" },
    binary:      { name: "bytea" },
    boolean:     { name: "boolean" },
    xml:         { name: "xml" },
    tsvector:    { name: "tsvector" },
    hstore:      { name: "hstore" },
    inet:        { name: "inet" },
    cidr:        { name: "cidr" },
    macaddr:     { name: "macaddr" },
    uuid:        { name: "uuid" },
    json:        { name: "json" },
    jsonb:       { name: "jsonb" },
    ltree:       { name: "ltree" },
    citext:      { name: "citext" },
    point:       { name: "point" },
    line:        { name: "line" },
    lseg:        { name: "lseg" },
    box:         { name: "box" },
    path:        { name: "path" },
    polygon:     { name: "polygon" },
    circle:      { name: "circle" },
    bit:         { name: "bit" },
    bit_varying: { name: "bit varying" },
    money:       { name: "money" },
    interval:    { name: "interval" },
    oid:         { name: "oid" },
  }

Mysql

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L41-L55

NATIVE_DATABASE_TYPES = {
    primary_key: "bigint auto_increment PRIMARY KEY",
    string:      { name: "varchar", limit: 255 },
    text:        { name: "text", limit: 65535 },
    integer:     { name: "int", limit: 4 },
    float:       { name: "float" },
    decimal:     { name: "decimal" },
    datetime:    { name: "datetime" },
    timestamp:   { name: "timestamp" },
    time:        { name: "time" },
    date:        { name: "date" },
    binary:      { name: "blob", limit: 65535 },
    boolean:     { name: "tinyint", limit: 1 },
    json:        { name: "json" },
  }

Upvotes: 1

Related Questions