Sign In

Rails migration data types

Rails TypeMySQLPostgreSQLSQLite
:binaryblobbyteablob
:booleantinyint(1)booleanboolean
:datedatedatedate
:datetimedatetimetimestampdatetime
:decimaldecimaldecimaldecimal
:floatfloatfloatfloat
:integerint(11)integerinteger
:stringvarchar(255)character varyingvarchar(255)
:texttexttexttext
:timetimetimedatetime
:timestampdatetimetimestampdatetime

Additional notes:

  1. The :string type in Rails typically maps to varchar(255) in MySQL and SQLite, while in PostgreSQL it’s generally character varying.
  2. For larger integers, Rails also provides a :bigint type, which maps to bigint in most databases.
  3. Rails migrations allow you to specify additional options like limit, precision, and scale for certain data types to further customize their behavior across different database systems.
  4. When using migrations, Rails abstracts away many of the database-specific details, allowing you to write database-agnostic code in most cases.
  5. For more specific or advanced data types, you may need to use database-specific syntax within your migrations.
  6. 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.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *