shiva kumar
shiva kumar

Reputation: 11424

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

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.

Upvotes: 51

Views: 16056

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176412

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.

Upvotes: 62

Related Questions