Rails migration data types
Rails Type | MySQL | PostgreSQL | SQLite |
---|---|---|---|
:binary | blob | bytea | blob |
:boolean | tinyint(1) | boolean | boolean |
:date | date | date | date |
:datetime | datetime | timestamp | datetime |
:decimal | decimal | decimal | decimal |
:float | float | float | float |
:integer | int(11) | integer | integer |
:string | varchar(255) | character varying | varchar(255) |
:text | text | text | text |
:time | time | time | datetime |
:timestamp | datetime | timestamp | datetime |
Additional notes:
- The
:string
type in Rails typically maps tovarchar(255)
in MySQL and SQLite, while in PostgreSQL it’s generallycharacter varying
. - For larger integers, Rails also provides a
:bigint
type, which maps tobigint
in most databases. - Rails migrations allow you to specify additional options like
limit
,precision
, andscale
for certain data types to further customize their behavior across different database systems. - When using migrations, Rails abstracts away many of the database-specific details, allowing you to write database-agnostic code in most cases.
- For more specific or advanced data types, you may need to use database-specific syntax within your migrations.
- The
schema.rb
file in Rails projects provides a database-independent representation of your schema, which can be useful for portability across different database systems.
Remember that while Rails tries to provide a consistent interface across different databases, there may be some subtle differences in behavior or performance characteristics for certain data types depending on the underlying database system.