是否有 Rails 列类型的文档? [英] Is there documentation for the Rails column types?

查看:30
本文介绍了是否有 Rails 列类型的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找的不仅仅是简单的类型列表 本页:

I'm looking for more than the simple type listing that is found on this page:

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean

但是是否有任何文档实际定义这些字段?

But is there any documentation that actually defines these fields?

特别是:

  • :string:text 有什么区别?
  • 介于 :float:decimal 之间?
  • :time:timestamp:datetime 的区别是什么?
  • What's the difference between :string and :text?
  • Between :float and :decimal?
  • What are the distinguishing features of :time, :timestamp, and :datetime?

是否在任何地方记录了这些类型的细微差别?

Are the nuances of these types documented anywhere?

DB 平台实现的要点与我要问的问题无关. 如果说 :datetime 没有定义的预期含义在 Rails 文档中,那么 db-adapter-writers 在选择相应的列类型时会遵循什么?

Points of DB-platform implementations are irrelevant to the question I'm trying to ask. If, say, :datetime does not have a defined intended meaning in Rails documentation, then what do db-adapter-writers go by when choosing a corresponding column type?

推荐答案

根据个人经验构建的指南:

Guidelines built from personal experience:

  • 字符串:
  • 限制为 255 个字符(取决于 DBMS)
  • 用于短文本字段(姓名、电子邮件等)
  • 文字:
  • 无限长度(取决于 DBMS)
  • 用于评论、博客文章等.一般经验法则:如果它是通过 textarea 捕获的,则使用 Text.对于使用文本字段的输入,请使用字符串.
  • 整数:
  • 整数
  • 浮动:
  • 以浮点精度存储的十进制数
  • 精度是固定的,这对于某些计算来说可能是有问题的;由于四舍五入不准确,通常不利于数学运算.
  • 十进制:
  • 以精确度存储的十进制数,根据您的计算需要而变化;将这些用于需要准确的数学
  • 请参阅这篇帖子了解示例和深入解释关于浮点数和小数之间的差异.
  • 布尔值:
  • 用于存储真/假属性(即只有两种状态的事物,如开/关)
  • 二进制:
  • 用于将图像、电影和其他文件以其原始原始格式存储在称为 blob
  • 的数据块中
  • :primary_key
  • 此数据类型是一个占位符,Rails 会将其转换为您选择的数据库所需的任何主键数据类型(即 postgreSQL 中的 serial primary key).它的使用有些复杂,不推荐使用.
  • 使用模型和迁移约束(如带有 :unique => true 选项的 validates_uniqueness_ofadd_index)来模拟主键功能在您自己的领域之一.
  • 日期:
  • 仅存储日期(年、月、日)
  • 时间:
  • 仅存储时间(小时、分钟、秒)
  • 日期时间:
  • 同时存储日期和时间
  • 时间戳
  • 同时存储日期和时间
  • 注意:就 Rails 而言,Timestamp 和 DateTime 的含义相同(使用任一类型来存储日期和时间).有关为何两者都存在的 TL;DR 描述,请阅读底部段落.
  • String:
  • Limited to 255 characters (depending on DBMS)
  • Use for short text fields (names, emails, etc)
  • Text:
  • Unlimited length (depending on DBMS)
  • Use for comments, blog posts, etc. General rule of thumb: if it's captured via textarea, use Text. For input using textfields, use string.
  • Integer:
  • Whole numbers
  • Float:
  • Decimal numbers stored with floating point precision
  • Precision is fixed, which can be problematic for some calculations; generally no good for math operations due to inaccurate rounding.
  • Decimal:
  • Decimal numbers stored with precision that varies according to what is needed by your calculations; use these for math that needs to be accurate
  • See this post for examples and an in-depth explanation on the differences between floats and decimals.
  • Boolean:
  • Use to store true/false attributes (i.e. things that only have two states, like on/off)
  • Binary:
  • Use to store images, movies, and other files in their original, raw format in chunks of data called blobs
  • :primary_key
  • This datatype is a placeholder that Rails translates into whatever primary key datatype your database of choice requires (i.e. serial primary key in postgreSQL). Its use is somewhat complicated and not recommended.
  • Use model and migration constraints (like validates_uniqueness_of and add_index with the :unique => true option) instead to simulate primary key functionality on one of your own fields.
  • Date:
  • Stores only a date (year, month, day)
  • Time:
  • Stores only a time (hours, minutes, seconds)
  • DateTime:
  • Stores both date and time
  • Timestamp
  • Stores both date and time
  • Note: For the purposes of Rails, both Timestamp and DateTime mean the same thing (use either type to store both date and time). For the TL;DR description of why both exist, read the bottom paragraph.

这些是经常存在混淆的类型;我希望这有帮助.我真的不知道为什么没有关于这些的官方文档.另外,我想您提到的这些数据库适配器是由编写 Rails 的同一个人编写的,因此他们在编写适配器时可能不需要任何文档.希望这会有所帮助!

These are the types about which confusion often exists; I hope this helps. I really don't know why there isn't official documentation about these. Also, I imagine these database adapters you referred to were written by the same people who wrote Rails, so they probably didn't need any documentation to go by when they were writing the adapters. Hope this helps!

注意::DateTime:Timestamp 的存在,据我所知,包含在 Rails 中,主要是为了与数据库系统兼容.例如,MySQL 的 TIMESTAMP 数据类型存储为 unix 时间戳.它的有效范围从 1970 年到 2038 年,时间存储为自上次 epoch,这应该是标准的,但实际上可能因系统而异.MySQL 后来引入了 DATETIME 数据类型,它存储为自1000-01-01 00:00:00"以来的秒数(从 5.6.4 起可选小数秒),代价是大小增加.保留 TIMESTAMP 数据类型是为了向后兼容.其他数据库系统也经历了类似的演变.Rails 认识到存在多种标准,并为两者提供了接​​口.但是,Rails ActiveRecord 默认将 :Timestamp:DateTime 都设置为存储在 MySql 的 DATETIME 中的 UTC 日期,因此它对 Rails 程序员没有功能差异.这些存在是为了希望区分两者的用户可以这样做.(更深入的解释,请参见 this 所以答案).

Note: the presence of both :DateTime and :Timestamp, from what I can find, is included by Rails mostly for compatibility with database systems. For instance, MySQL's TIMESTAMP datatype is stored as a unix timestamp. Its valid range goes from 1970 to 2038, and the time is stored as the number of seconds that have elapsed since the last epoch, which is supposedly standard, but in practice can differ from system to system. MySQL later introduced the DATETIME datatype, which is stored as seconds (with optional fractional seconds as of 5.6.4) since "1000-01-01 00:00:00", at the cost of a size increase. The TIMESTAMP datatype was retained for backwards compatibility. Other database systems went through similar evolutions. Rails recognized that multiple standards existed, and provided interfaces to both. However, Rails ActiveRecord defaults both :Timestamp and :DateTime to UTC dates stored in MySql's DATETIME, so it makes no functional difference to Rails programmers. These exist so that users who wish to differentiate between the two can do so. (For a more in-depth explanation, see this SO answer).

这篇关于是否有 Rails 列类型的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆