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
But is there any documentation that actually defines these fields?
Specifically:
- What’s the difference between
:stringand:text? - Between
:floatand:decimal? - What are the distinguishing features of
:time,:timestamp, and:datetime?
Are the nuances of these types documented anywhere?
EDIT: 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:
serial primary keyin postgreSQL). Its use is somewhat complicated and not recommended.validates_uniqueness_ofandadd_indexwith the:unique => trueoption) instead to simulate primary key functionality on one of your own fields.These are the types about which confusion often exists. 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.
Note: the presence of both
:DateTimeand:Timestamp, from what I can find, is included by Rails mostly for compatibility with database systems. For instance, MySQL’sTIMESTAMPdatatype 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 theDATETIMEdatatype, 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. TheTIMESTAMPdatatype 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:Timestampand:DateTimeto UTC dates stored in MySql’sDATETIME, 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).