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
:stringtype in Rails typically maps tovarchar(255)in MySQL and SQLite, while in PostgreSQL it’s generallycharacter varying. - For larger integers, Rails also provides a
:biginttype, which maps tobigintin most databases. - Rails migrations allow you to specify additional options like
limit,precision, andscalefor 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.rbfile 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.
