Rails 8.1 now sorts table columns alphabetically when dumping the schema

schema.rb documents the database structure in a database-agnostic way, providing a Ruby representation of the schema within the limits of what Active Record can express.

Before

When running migrations, Rails generates the schema.rb file by dumping the current database structure. However, the order of columns in schema.rb reflects the physical column order in the database, which typically follows migration history.

Imagine you have a posts table:

create_table "posts", force: :cascade do |t|
  t.string "title"
  t.boolean "published"
  t.text "summary"
end

After applying different migrations, the order of columns may differ on other machines:

create_table "posts", force: :cascade do |t|
  t.string "title"
  t.text "summary"
  t.boolean "published"
end

This can lead to merge conflicts when two branches modify the same table concurrently, creating confusion and clutter, particularly when several developers are working together.

After

Starting with Rails 8.1, table columns are sorted alphabetically when dumping the schema, ensuring a consistent order. This behavior is not configurable and cannot be disabled.

create_table "posts", force: :cascade do |t|
  t.boolean "published"
  t.text "summary"
  t.string "title"
end

Upgrading to Rails 8.1

When upgrading an existing application to Rails 8.1, running rails db:schema:dump will reorder all columns alphabetically. This may result in a large diff in your first commit after the upgrade. This is expected and a one-time change.

References

Need help on your Ruby on Rails or React project?

Join Our Newsletter