What is the difference between rails structure.sql and schema.rb

The main difference is that schema.rb is a Ruby representation of the database, and it is generally database-agnostic. structure.sql instead is a SQL representation of the database and it depends on the specific database you selected.

You only use the structure if you have specific database features that you need and that can’t be represented by the schema.rb. For example, in the past some people replaced schema.rb with structure.sql in order to use PostgreSQL JSONB fields or foreign key constraints at database level.

Both features are now supported in the migrations, therefore you don’t need to switch to structure.sql anymore (in these cases).

In general, I suggest you to use the schema.rb.

Leave a Comment