在新的 Rails 应用程序中使用旧数据库时出现问题
在新的 Rails 应用程序中使用旧数据库时,常见的问题/绊脚石/问题/缺点是什么?
我们必须决定是使用旧数据库还是编写迁移脚本,按照 Rails 约定将数据从旧数据库转移到新数据库。你有什么建议?
What are the usual problems / stumbling blocks / issues / drawbacks when using a legacy database with a new rails application?
We have to decide between using an old database or writing migration scripts to bring data from old database to new database following rails conventions. What would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表命名让我困扰了一段时间。诀窍是在您的模型中使用它:
这样您就可以在链接到您想要的任何表时使用您想要的任何模型名称。
Table naming was one that got me for a while. The trick was to use this in your models:
That way you can use whatever model name you want while linking to any table you want.
1)通常第一个问题是数据库模式设计具有复合主键(多列键),其中Rails(或至少ActiveRecord)希望主键名为“id”。许多好的数据模型不使用代理键,而是使用自然键,因此无法避免复合键。实际上,在为 ORM 设计新数据库时,使用名为“id”的代理键更为实用,但通过始终在自然键上包含备用键约束/索引来强制数据完整性。
2) 使用复数与单数的表命名(Rails 希望将复数映射到其域对象。对于许多数据库来说,这可以通过同义词轻松克服。
这是我在使用 Rails 和其他 MVC 框架时遇到的两个问题,但有些在过去的几年里已经发生了变化,并为简单的废话提供了替代方案。更改遗留数据库的成本很高,而强制执行命名约定是一个很大的错误,我相信现在已经学到了这一点。
1) Typically the first issue is the database schema design has composite primary keys (multi column keys), where Rails (or at least ActiveRecord) wants primary keys named "id". Many good data models don't use surrogate keys, they use natural keys, so they cannot avoid composite keys. In practice, when designing a new database for an ORM, it is more practical to use surrogate keys named "id", but enforce data integrity by always including an alternate key constraint / index on the natural key.
2) Table naming that uses plural vs singular (Rails wants plural to map to its domain objects. With many databases this can be overcome easily with synonyms.
Those are the two issues I've run into with Rails and other MVC frameworks, but some have changed in the past couple of years and provided alternatives to simplistic nonsense. It is expensive to change a legacy database, and enforcing a naming convention is a big mistake which I believe has been learned now.