外键约束在 rake 期间不会复制到 testdb 中

发布于 2024-10-22 03:30:43 字数 80 浏览 18 评论 0原文

这是我无法调试的一个问题。在执行 rake db:test:clone_struct 时,未将外键从开发数据库复制到测试数据库。我有什么遗漏的吗?

Here is one issue which I am unable to debug. While doing an rake db:test:clone_structure, the foreign keys that are not copied from development database to test database. Is there anything that I am missing?

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

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

发布评论

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

评论(1

爱*していゐ 2024-10-29 03:30:43

你的问题是Rails(或ActiveRecord)不理解数据库内的外键,也不理解CHECK约束或任何比唯一索引更奇特的东西。一般来说,Rails 工作起来很愉快,但有时 Rails 中的态度多于良好的判断力。

Foreigner 用于向 ActiveRecord 添加 FK 支持,但不了解 Oracle。您也许能够调整 PostgreSQL 对 Oracle 的支持,但我不了解 Oracle,所以这可能不是一个好主意。 foreigner 还不支持 CHECK 约束。

快速解决方案是将 FK 和 CHECK 转储为原始 SQL,并将该 SQL 注入到测试和生产数据库中。然后编写一个快速脚本,执行 rake db:test:clone_struct 操作,然后进行原始 SQL FK 和 CHECK 复制。

抱歉,没有简单的方法可以做到这一点,但是一旦你超出了框架想要做的事情的范围,事情就会变得丑陋(框架越全面,事情就会变得越丑陋)。不过,围绕通常的 rake 命令的一些 SQL 争论并不是那么令人讨厌。

Your problem is that Rails (or ActiveRecord) doesn't understand foreign keys inside the database, nor does it understand CHECK constraints or anything else fancier than a unique index. Rails is generally nice to work with but sometimes there is more attitude than good sense in Rails.

There is Foreigner for adding FK support to ActiveRecord but that doesn't know about Oracle. You might be able to adapt the PostgreSQL support to Oracle but I don't know my way around Oracle so that might not be a good idea. Foreigner also doesn't support CHECK constraints (yet).

The quick solution would be to dump the FKs and CHECKs as raw SQL and pump that SQL into your test and production databases. Then wrap a quick script around that that does a rake db:test:clone_structure followed by the raw SQL FK and CHECK copying.

Sorry that there's no easy way to do this but once you get outside the bounds of what a framework wants to do things get ugly (and the more comprehensive the framework, the uglier things get). A little bit of SQL wrangling wrapped around the usual rake command isn't that nasty though.

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