ActiveRecord 数据类型的文档页面在哪里?

发布于 2024-09-27 12:57:12 字数 48 浏览 6 评论 0原文

我找不到包含所有数据类型列表的活动记录文档页面。

有人可以帮我吗?

I can't find the active record documenation page that has a list of all the data types.

Can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窗影残 2024-10-04 12:57:12

如果您正在谈论迁移的类型,例如字符串、整数、日期时间等,那么您需要 ActiveRecord::ConnectionAdapters::TableDefinition方法。 (Rails 5 编辑:另请参阅连接。add_column 。)

自此更新以来,标准类型为:

  • :primary_key
  • :string
  • :text
  • :整数
  • :bigint
  • :float
  • :小数
  • :数字
  • :日期时间 >
  • :time
  • :date
  • :binary
  • :boolean

:decimal 的实现是每个数据库都不同,所以如果可能的话我会避免它。只要您的数据库支持,您就可以使用此列表中不包含的类型(例如 MySQL 中的 :polygon),但这与数据库无关,也应该避免。

If you're talking about the types for migrations, e.g. string, integer, datetime, etc, then you want ActiveRecord::ConnectionAdapters::TableDefinition, the column method. (Rails 5 edit: see also connection.add_column.)

As of this update, the standard types are:

  • :primary_key
  • :string
  • :text
  • :integer
  • :bigint
  • :float
  • :decimal
  • :numeric
  • :datetime
  • :time
  • :date
  • :binary
  • :boolean

The implementation of :decimal is different with each database, so I'd avoid it if possible. You may use a type not in this list as long as it is supported by your database (for example, :polygon in MySQL), but this will not be database agnostic and should also be avoided.

穿越时光隧道 2024-10-04 12:57:12

您还可以在源中查看ActiveRecord 数据类型。每个 DBMS 适配器都包含其自己的映射。例如,在 MySQL 的情况下,查看此文件: https:/ /github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148 或通过当前 DBMS 适配器的这行代码获取它:

ActiveRecord::Base.connection.native_database_types.keys

You can also see ActiveRecord data types in sources. Each DBMS adapter contains its own mapping. For example, in MySQL case look at this file: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148 or get it by this line of code for current DBMS adapter:

ActiveRecord::Base.connection.native_database_types.keys
羁客 2024-10-04 12:57:12

以下是数据库适配器类型的默认映射:

在此处输入图像描述
在此处输入图像描述

Here is the default mappings of types for database adapters:

enter image description here
enter image description here

静谧 2024-10-04 12:57:12

请注意,这是基于 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" },
      }

Note this is based on Rails Source Code dated 13th Feb 2015 (Rails
4.2)

In case someone wants to see how these datatypes get mapped into the database you are using.

You can grab easily at rails source code at github.

For example

Rails data types to mysql datatyes mapping.

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

Found at https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148

And if some one wants postgreSQL here you go.

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" },
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文