Ruby-on-rails ActiveRecord 数据类型的文档页面在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3956186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the documentation page for ActiveRecord data types?
提问by Blankman
I can't find the active record documenation page that has a list of all the data types.
我找不到包含所有数据类型列表的活动记录文档页面。
Can someone help me out?
有人可以帮我吗?
回答by Mark Thomas
If you're talking about the types for migrations, e.g. string, integer, datetime, etc, then you want ActiveRecord::ConnectionAdapters::TableDefinition, the columnmethod. (Rails 5 edit: see also connection.add_column.)
如果您正在谈论迁移的类型,例如字符串、整数、日期时间等,那么您需要ActiveRecord::ConnectionAdapters::TableDefinition,即列方法。(Rails的5编辑:又见连接add_column。)
As of this update, the standard types are:
截至本次更新,标准类型为:
:primary_key:string:text:integer:bigint:float:decimal:numeric:datetime:time:date:binary:boolean
:primary_key:string:text:integer:bigint:float:decimal:numeric:datetime:time:date:binary:boolean
The implementation of :decimalis 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, :polygonin MySQL), but this will not be database agnostic and should also be avoided.
:decimal每个数据库的实现都不同,所以如果可能的话,我会避免它。您可以使用不在此列表中的类型,只要它受您的数据库支持(例如,:polygon在 MySQL 中),但这与数据库无关,也应避免使用。
回答by Psylone
You can also see ActiveRecord data typesin 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#L148or get it by this line of code for current DBMS adapter:
您还可以在源中查看ActiveRecord 数据类型。每个 DBMS 适配器都包含自己的映射。例如,在 MySQL 的情况下查看此文件:https: //github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148或通过这行代码获取当前数据库管理系统适配器:
ActiveRecord::Base.connection.native_database_types.keys
回答by gotqn
Here is the default mappings of types for database adapters:
以下是数据库适配器类型的默认映射:




回答by MadNik
Note this is based on Rails Source Code dated 13th Feb 2015 (Rails 4.2)
请注意,这是基于 2015 年 2 月 13 日的 Rails 源代码(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.
您可以在 github 上轻松获取 rails 源代码。
For example
例如
Rails data types to mysql datatyes mapping.
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 }
}
And if some one wants postgreSQL here you go.
如果有人想要 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" },
}

