Ruby on Rails 和 db:fixtures:load - 它可以忽略某些模型吗?

发布于 2024-08-19 16:25:07 字数 394 浏览 11 评论 0原文

我在 Ruby on Rails 应用程序中使用了两个数据库;第一个是应用程序的数据库,第二个是 Rails 不受控制的独立数据库。

问题是,当将装置加载到开发中时,它尝试从与开发数据库的连接对独立数据库中的表运行 DELETE 语句,这显然会出错。

我不希望 Rails 尝试做任何事情,但读取独立数据库 - 我特别不希望它尝试删除表。

有没有一种简单的方法可以告诉 Rails 在加载装置时忽略第二个数据库的模型?

更新:为了澄清,Rails 似乎认为独立数据库中的表是开发连接的一部分,尽管我已经使用 external_connection 在模型类中指定了正确的连接。另外需要注意的是,所有模型类都按照 script/console 的要求精确工作。

I have two databases in use in a Ruby on Rails application; one is the database for the application while the second is an independent database over which Rails is not given control.

Problem is when loading fixtures into the dev, it tries to run DELETE statements on the tables in the independent database from the connection to the dev database, which obviously errors out.

I don't want Rails to try to do ANYTHING but read the independent database - I especially don't want it trying to delete tables.

Is there a simple way to tell Rails to ignore the models for the second database when loading fixtures?

UPDATE: To clarify, Rails seems to think the tables from the independent database are part of the development connection, though I have specified the correct connection in the model class using establish_connection. As another note, all model classes work precisely as desired from script/console.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云之铃。 2024-08-26 16:25:07
rake db:fixtures:load RAILS_ENV=testing

将为您的 database.yml 中配置为 testing 的数据库执行此操作

rake db:fixtures:load RAILS_ENV=testing

will do the job for database configured as testing in your database.yml

倚栏听风 2024-08-26 16:25:07

我认为您也可以通过将独立数据库中的所有表添加到environment.rb中的ActiveRecord::SchemaDumper.ignore_tables来实现此目的,如下所示:

ActiveRecord::SchemaDumper.ignore_tables = ['independent_db_table1', 'independent_db_table2']

I think you might also be able to accomplish this by adding all the tables in the independent database to ActiveRecord::SchemaDumper.ignore_tables in environment.rb, like so:

ActiveRecord::SchemaDumper.ignore_tables = ['independent_db_table1', 'independent_db_table2']
超可爱的懒熊 2024-08-26 16:25:07

好吧...我的问题是我使用 script/generate 从辅助数据库创建模型,该数据库还创建了固定装置、架构、测试和迁移文件。我已经删除了架构、测试和迁移文件,但是没有删除了生成的装置(它们是空文件),因为我认为它没有创建任何装置。

从辅助数据库中删除所有文件(包括模型)并重新运行开发数据库的迁移后,我添加回中的模型文件和数据库<来自辅助数据库的 code>databases.yml ,解决了问题。

我仍然无法解释为什么 Rails 的 rake 任务在错误的数据库中查找,并且我对 Rails 在辅助数据库中添加 schema_migrations 表感到有点失望,它显然不需要该表。

然而,现在它可以工作了。

Okay...my issue was that I used the script/generate to create the models from the secondary database, which also created fixture, schema, test, and migrations files. I had removed the schema, test, and migrations files, but did not remove the generated fixtures (they were empty files) as I did not think it had created any.

After removing all files (including the models) from the secondary database and re-running the migrations for the dev db, I added back only the model files and the database in databases.yml from the secondary db, which solved the issue.

I still can't explain why Rails' rake tasks were looking in the wrong database and I am a little disappointed with rails' addition of the schema_migrations table in the secondary database, which it obviously does not need.

However, it works now.

东风软 2024-08-26 16:25:07

删除 test/fixtures 目录中的 model_name.yml 文件,Rails 不会尝试删除这些表。

更好的是,删除所有 *.yml 文件并停止 使用 完全固定装置。

Delete the model_name.yml file in your test/fixtures directory and Rails will not try to delete these tables.

Better yet, delete all your *.yml files and stop using fixtures entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文