Ruby-on-rails 丢失了我的 schema.rb!可以再生吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9731585/
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
Lost my schema.rb! Can it be regenerated?
提问by brad
Due to some deployment issues I stopped tracking schema.rb in git. Somehow I have stuffed this up and somewhere along the way my schema.rb file has disappeared.
由于一些部署问题,我停止在 git 中跟踪 schema.rb。不知何故,我已经把它塞满了,而且我的 schema.rb 文件已经消失了。
Is there a way of regenerating schema.rb from the database or from the migrations? I would prefer not to lose the existing data.
有没有办法从数据库或迁移中重新生成 schema.rb?我不想丢失现有数据。
回答by mguymon
If you run a rake -Tit will list all possible rake tasks for your Rails project. One of them is db:schema:dumpwhich will recreate the schema.rb for the Rails app from the database.
如果您运行 arake -T它将列出您的 Rails 项目的所有可能的 rake 任务。其中之一是db:schema:dump,它将从数据库中为 Rails 应用程序重新创建 schema.rb。
bundle exec rake db:schema:dump
回答by gamov
Careful,
小心,
rake db:schema:dump
will dump the current DB schema FROM the DB. This means that if you made any changes to your migrations, they will NOTbe reflected in schema.rb file which is not what you want IMO.
将转储当前数据库架构从数据库中。这意味着如果您对迁移进行了任何更改,它们将不会反映在 schema.rb 文件中,这不是您想要的 IMO。
If you want to re-create the schema from the migrations, do the following:
如果要从迁移重新创建架构,请执行以下操作:
rake db:drop # ERASES THE DATABASE !!!!
rake db:create
rake db:migrate
回答by pschuegr
rake db:schema:dump
I think this is still valid in Rails 3 - it regenerates the schema.rb from the database.
我认为这在 Rails 3 中仍然有效 - 它从数据库中重新生成 schema.rb。
回答by Kaleem Ullah
RAILS 5 Way:
导轨 5 种方式:
rails db:schema:dump
or if you Encounter Gem::LoadErrorthen:
或者如果您遇到Gem::LoadError则:
bundle exec rails db:schema:dump
Note:
笔记:
in rails 5 it is recommended that task are generated/executed by using railsinstead of rake, this is just to remember, rails generated task are of extension .rakesee in lib/tasks/myTask.rake. which means these task can also be executed by prepending rake.
在 rails 5 中,建议使用rails而不是生成/执行任务rake,这只是要记住,rails 生成的任务是扩展名,.rake参见lib/tasks/myTask.rake。这意味着这些任务也可以通过 prepending 来执行rake。
回答by Graham Swan
If you regenerate schema.rblocally, you should be alright. It simply holds a representation of the structure of your database tables. The data itself is not contained in this file.
如果你schema.rb在本地再生,你应该没问题。它只是保存数据库表结构的表示。数据本身不包含在此文件中。
To regenerate your schema.rbfile, run:
要重新生成schema.rb文件,请运行:
bundle exec rake db:schema:dump
Then simply commit the new schema.rbfile and you should be in good shape!
然后只需提交新schema.rb文件,您应该处于良好状态!
回答by Colin Summers
Directly from the schema.rb file itself:
直接从 schema.rb 文件本身:
If you need to create the application database on another system, you should be using
db:schema:load, not running all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations you'll amass, the slower it'll run and the greater likelihood for issues).
如果您需要在另一个系统上创建应用程序数据库,您应该使用
db:schema:load,而不是从头开始运行所有迁移。后者是一种有缺陷且不可持续的方法(您积累的迁移越多,运行速度就越慢,出现问题的可能性就越大)。
So do NOT do the suggestion of rake db:migrate, which was suggested in the - at the time of this writing - lowest rated answer.
所以不要做 的建议rake db:migrate,这是在撰写本文时 - 评分最低的答案中提出的。
回答by R.Cha
I also had a similar problem where my old schema was not refreshing even if I deleted migration.
我也有一个类似的问题,即使我删除了迁移,我的旧架构也没有刷新。
So, what I did was dropping all existing tables in the database and migrating them again. Then running "db:schema:load" command gave me a fresh schema.rb.
所以,我所做的是删除数据库中的所有现有表并再次迁移它们。然后运行“db:schema:load”命令给了我一个新的schema.rb。
drop table my_table_name // deleted them indivisully
rake db:migrate
rake db:schema:dump // re-created a new schema

