ActiveRecord DB数据类型列表作为符号/字符串

时间:2017-03-13 15:41:15

标签: ruby-on-rails rails-activerecord ruby-on-rails-5

这是一个非常简短的问题:是否有可用于ActiveRecord的mysql数据库中列的所有数据类型的列表?

我知道数据类型并且它们都列在文档中,但我想知道是否已经有一个类型的集合,我可以使用它来填充html <select> 。如果它已经存在,我想避免为此创建一个自己的列表,但我似乎无法找到它。

1 个答案:

答案 0 :(得分:1)

您可以使用此命令(使用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的:

您还可以浏览源代码并获取此大规模阵列。

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" },
  }