43
votes

I am aware that schema.rb file is a ruby file and it get created and modified when a rake migration is run, but what about the structure.sql.

I have seen in some projects schema.rb and in others structure.sql and in some both files, where do they configure which file to create.

What exactly is the difference between the two.

Is structure.sql generated is specific to a particular DB.

1
Also good info here: edgeguides.rubyonrails.org/…Yarin
Also, be aware that Heroku might not accept structure.sql... SEE rubyofftherails.blogspot.com/2016/02/… and help.heroku.com/AHH2KNSR/…Yarin

1 Answers

54
votes

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.