An image showcasing the integration of SQL in a Rails database, featuring Rails elements symbolized by ruby-colored gemstones and SQL represented by query language symbols and database icons. The interaction between Rails and SQL is central, against a backdrop of digital patterns and code snippets. The color palette merges Rails reds with database blues and greens, illustrating the technical synergy in a Rails SQL environment.
Rails and SQL: A Harmonious Database Symphony” — Visualizing the seamless integration of SQL within the Rails ecosystem.

Choosing :sql over :ruby for schema_format in Rails

Alessio Bussolari
2 min readDec 19, 2023

--

In Ruby on Rails, the configuration option config.active_record.schema_format determines the format for dumping the database schema. It can be set to either :ruby (using Ruby DSL) or :sql (using plain SQL). While :ruby is the default and works well for many applications, there are compelling reasons to consider :sql, particularly in more complex setups.

Understanding the Differences

:ruby Format:

  • Uses Ruby DSL to generate schema.rb.
  • Is database-agnostic, making it suitable for simpler applications or when switching between different DBMS (Database Management Systems).

:sql Format:

  • Generates structure.sql using raw SQL statements.
  • Captures all database-specific elements like triggers, views, stored procedures, and complex indexes.

Advantages of :sql Format

  1. Database Specific Features:
    If your application uses database-specific features like foreign key constraints, advanced indexing, triggers, or stored procedures, :sql ensures these are accurately captured in the schema file. The Ruby DSL used in schema.rb can’t always express these complex database features.
  2. Complex Indexes

--

--

Alessio Bussolari

Ruby on Rails programmer since 2009. Current CTO at COSMIC SRL, where I lead the team in creating innovative solutions.