Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/34394823/what-is-the-difference-between-rails-structure-sql-and-schema-rb

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...