Rails:schema.rb 有什么作用?

编程入门 行业动态 更新时间:2024-10-11 13:26:25
本文介绍了Rails:schema.rb 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我以前认为Rails项目中的db/schema.rb存储的是数据库schema,这样ActiveRecord就能知道什么表/column 它有.

I used to think the db/schema.rb in a Rails project stored the database schema, so that ActiveRecord can know what table/column it has.

但早些时候我惊讶地发现我的项目在删除db/schema.rb后正常运行!

But earlier I surprisingly noticed that my project runs normally after I delete db/schema.rb!

那么,既然 Rails 可以在没有它的情况下工作,那么 schema.rb 到底能做什么?

So, since the Rails can work without it, what does schema.rb really do?

推荐答案

schema.rb 主要有两个目的:

  • 它记录了数据库模式的最终当前状态.通常,尤其是当您有多个迁移时,很难仅从迁移中推断出模式.有了现有的 schema.rb,你可以看看那里.ActiveRecord 本身确实不会使用它.它将在运行时内省数据库,因为这比期望用户保持 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 deduce 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 the schema.rb up-to-date. However to avoid confusion of your developers, you should always maintain a file that is up-to-date with your migrations.

    测试使用它来填充数据库架构.因此,rake db:schema:dump 通常作为 rake test:prepare 运行的一部分运行.目的是测试数据库的模式与当前的开发数据库完全匹配.

    It is used by the tests to populate the database schema. As such a rake db:schema:dump is often run as part of the rake test:prepare run. The purpose is that the schema of the test database exactly matches the current development database.

  • 更多推荐

    Rails:schema.rb 有什么作用?

    本文发布于:2023-10-18 04:23:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1503089.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:有什么   作用   Rails   schema   rb

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!