ActiveRecord 数据类型文档在哪里?

75

我找不到活动记录文档页面,其中列出了所有数据类型。

有人可以帮我吗?

4个回答

107
如果您谈论迁移类型,例如字符串、整数、日期时间等,则需要使用 ActiveRecord::ConnectionAdapters::TableDefinitioncolumn 方法。(Rails 5编辑:还请参阅 connection.add_column。)
截至此更新时,标准类型包括:
  • :primary_key
  • :string
  • :text
  • :integer
  • :bigint
  • :float
  • :decimal
  • :numeric
  • :datetime
  • :time
  • :date
  • :binary
  • :boolean
由于每个数据库的实现方式不同,因此如果可能的话,我会避免使用 :decimal 类型。只要您的数据库支持它(例如 MySQL 中的 :polygon),您可以使用此列表中未列出的类型,但这样做将不具备数据库通用性,因此也应该避免使用。

@Swanand 用户创建的笔记在文档无法提供足够帮助的领域也非常有用。 - Ajedi32
请注意,这些在Rails 4中保持不变。 - Mark Thomas
:timestamp 似乎不再在列表中,并被视为 :datetime(在 db/schema.rb 中显示为 :datetime)。或者可能只是 :datetime 的别名(参见 https://github.com/rails/rails/blob/v4.2.1.rc4/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L364-366)。 - x-yuri
似乎 :timestamp 已从 Rails 4.2+ 中移除。 - Mark Thomas
请注意,类型的文档现已移动至:http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_column - Mark Thomas
显示剩余2条评论

6

5

请注意,这是基于2015年2月13日的Rails源代码(Rails 4.2)。

如果有人想看看这些数据类型如何映射到他们正在使用的数据库中,可以轻松地在github上获取rails源代码。

例如:

Rails数据类型到mysql数据类型的映射。

NATIVE_DATABASE_TYPES = {
        :primary_key => "int(11) auto_increment PRIMARY KEY",
        :string      => { :name => "varchar", :limit => 255 },
        :text        => { :name => "text" },
        :integer     => { :name => "int", :limit => 4 },
        :float       => { :name => "float" },
        :decimal     => { :name => "decimal" },
        :datetime    => { :name => "datetime" },
        :time        => { :name => "time" },
        :date        => { :name => "date" },
        :binary      => { :name => "blob" },
        :boolean     => { :name => "tinyint", :limit => 1 }
      }

在这里找到:https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148

如果有人想要PostgreSQL,这是链接:

NATIVE_DATABASE_TYPES = {
        primary_key: "serial primary key",
        bigserial: "bigserial",
        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" },
        bigint:      { name: "bigint" },
        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" },
        bit:         { name: "bit" },
        bit_varying: { name: "bit varying" },
        money:       { name: "money" },
      }

5
以下是数据库适配器的默认类型映射: enter image description here enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接