ActiveRecord 数据类型的文档页面在哪里?
我找不到包含所有数据类型列表的活动记录文档页面。
有人可以帮我吗?
I can't find the active record documenation page that has a list of all the data types.
Can someone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您正在谈论迁移的类型,例如字符串、整数、日期时间等,那么您需要 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.您还可以在源中查看ActiveRecord 数据类型。每个 DBMS 适配器都包含其自己的映射。例如,在 MySQL 的情况下,查看此文件: https:/ /github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148 或通过当前 DBMS 适配器的这行代码获取它:
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:
以下是数据库适配器类型的默认映射:
Here is the default mappings of types for database adapters:
如果有人想了解这些数据类型如何映射到您正在使用的数据库中。
您可以在 github 上轻松获取 Rails 源代码。
例如
位于 https://github.com /rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L148
如果有人想要 postgreSQL,你就可以。
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
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.