Ruby-on-rails Rails:schema.rb 有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9884429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: what does schema.rb do?
提问by Lai Yu-Hsuan
I used to think the db/schema.rbin a Railsproject stored the database schema, so that ActiveRecordcan know what table/column it has.
我曾经认为db/schema.rb在一个Rails项目中存储了数据库模式,这样就ActiveRecord可以知道它有什么表/列。
But earlier I surprisingly noticed that my project runs normally after I delete db/schema.rb!
但早些时候我惊讶地注意到我的项目在我删除后正常运行db/schema.rb!
So, since the Railscan work without it, what does schema.rbreally do?
那么,既然Rails没有它也可以工作,那么schema.rb真正做什么呢?
回答by Holger Just
The schema.rbserves mainly two purposes:
该schema.rb主要有两个目的:
It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduct the schema just from the migrations alone. With a present
schema.rb, you can just have a look there. ActiveRecord itself will indeed not use it. It will introspect the database during runtime as this is much safer than to expect users to keep theschema.rbup-to-date. However to avoid confusion of your developers, you should always maintain a file that is up-to-date with your migrations.It is used by the tests to populate the database schema. As such a
rake db:schema:dumpis often run as part of the raketest:preparerun. The purpose is that the schema of the test database exactly matches the current development database.
它记录了数据库模式的最终当前状态。通常,尤其是当您有多个迁移时,很难仅从迁移中推断出架构。带着礼物
schema.rb,你可以看看那里。ActiveRecord 本身确实不会使用它。它将在运行时内省数据库,因为这比期望用户保持schema.rb最新状态要安全得多。但是,为了避免混淆您的开发人员,您应该始终维护一个与您的迁移保持同步的文件。测试使用它来填充数据库模式。因此,a
rake db:schema:dump通常作为佣金test:prepare运行的一部分运行。目的是测试数据库的模式与当前的开发数据库完全匹配。
回答by notapatch
Rails Documentation / 6.1 What are Schema Files for?
Rails 文档 / 6.1 Schema 文件的用途是什么?
Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either db/schema.rb or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.
There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.
For example, this is how the test database is created: the current development database is dumped (either to db/schema.rb or db/structure.sql) and then loaded into the test database.
Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is frequently spread across several migrations, but the information is nicely summed up in the schema file. The annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality.
迁移虽然强大,但并不是数据库模式的权威来源。该角色属于 db/schema.rb 或 Active Record 通过检查数据库生成的 SQL 文件。它们不是为编辑而设计的,它们只是代表数据库的当前状态。
无需(而且很容易出错)通过重放整个迁移历史来部署应用程序的新实例。将当前模式的描述加载到数据库中要简单得多,速度也更快。
例如,测试数据库是这样创建的:将当前的开发数据库转储(转储到 db/schema.rb 或 db/structure.sql),然后加载到测试数据库中。
如果您想快速查看 Active Record 对象具有哪些属性,架构文件也很有用。此信息不在模型的代码中,并且经常分布在多个迁移中,但该信息很好地总结在模式文件中。如果您需要该功能, annotate_models gem 会自动添加和更新每个模型顶部的注释,总结模式。

