加载 Rails 迁移的种子数据
我有一个现有的数据库,其中我将以前的“NULL”列转换为具有默认值的列(并用所述默认值填充该列)。但是,该值是我需要创建的记录的 ID。如果我将此记录放入 db/seeds.rb 中,它将不会运行,因为 db/seeds.rb 会在迁移后运行 - 但迁移需要种子数据。如果我将记录创建保留在迁移中,那么当我使用 db:load 创建新数据库时,我将不会获得记录。除了在 db/seeds.rb 和迁移中复制此内容之外,还有更好的方法吗?
谢谢!
I have an existing database in which I am converting a formerly 'NULL' column to one that has a default value (and populating that with said default value). However, that value is an ID of a record I need to create. If I put this record in db/seeds.rb
, it won't run because db/seeds.rb
runs after migrations -- but the migration needs seed data. If I leave the record creation in the migration, then I don't get the record if I make a fresh database with db:load
. Is there a better way other than duplicating this in both db/seeds.rb
and the migration?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然我可以理解您希望保持 DRY 的愿望,而不必在迁移和 seeds.rb 中都写这个,但我认为您应该在两个地方都写它。不仅仅是为了让它发挥作用,而是为了完成与您的问题相关的不同要求。
您需要确保无论外部进程如何,您的迁移都可以正确执行。这意味着您应该将所需的任何代码放入该特定迁移中。除了确保迁移正确执行之外,这并没有完成任何其他任务。假设其他人在不知道您将部分代码放入 seeds.rb 的情况下尝试迁移,他们将很难弄清楚发生了什么。
您可以通过在 seeds.rb 中包含类似的代码来使 db:load 正常工作。但是,您应该在 seeds.rb 中评估数据库的当前状态,因为它是在迁移后运行的。因此,您可以检查该列是否存在,以及默认值是什么等。这意味着,如果迁移运行并处理了所有事情,seeds.rb 就不会重复工作或不恰当地修改值。但是,如果迁移没有按预期设置这些变量,则可以设置这些值。
我建议将其视为两个单独的问题,这样您就可以更有信心每个问题都能独立成功地执行。它还为您自己或其他人了解未来发生的事情创造了更好的可维护性。
While I can understand your desire to stay DRY and not have to write this in both the migration and seeds.rb, I think you should write it in both places. Not just to make it work, but to accomplish different requirements related to your problem.
You need to ensure that your migration can execute properly regardless of external processes. That means you should put any code required within that specific migration. This isn't to accomplish anything besides making sure your migration executes properly. Suppose someone else tries to migrate without knowing you put part of the code in seeds.rb, it would be very difficult for them to figure out what's going on.
You can make db:load work properly by including similar code in seeds.rb. However, you should be evaluating the current state of your database in seeds.rb due to the fact that it runs after the migrations. So you can check to see if the column exists, and what the default value is etc. This means that if the migration ran and took care of everything, seeds.rb doesn't repeat work or modify values inappropriately. However, if the migration did not set these variables as expected, it is able to set the values.
I'd recommend looking at it as two separate issues so you can be more confident of each one executing successfully independent of one another. It also creates better maintainability for understanding by yourself or others of what's happening in the future.
在我看来,您应该在 db/seeds.rb 和迁移中都处理这个问题。
迁移用于将现有数据库从旧版本迁移到另一个版本,而seeds.rb 和schema.rb 用于最新版本的新数据库。
In my opinion you should treat this in both
db/seeds.rb
and the migration.The migration is used to get an existing database from an older version to another version while
seeds.rb
andschema.rb
are used for a fresh database with the latest version.