Rails 使用 MS SQL 服务器数据库

在我的 rails 应用程序中,我需要使用现有的 MS SQL 数据库.我已尝试遵循本手册:

In my rails app, I need to use an existing MS SQL database. I have tried following this manual:

http://rubyrailsandwindows.blogspot.com/2008/03/rails-2-and-sql-server-2008-on-windows_24.html

我不知道如何做模型、脚手架等,因为 rails 不能简单地使用另一个数据库.我不想为 MS SQL 数据库中的表运行 rake db:migrate.我还需要在模型中写什么?如果简单:使用 MS SQL 服务器数据库中的表,但我也想解决迁移等问题.

I do not know how to do models, scaffolds, etc because rails cannot simply use another db. I didn't want to run rake db:migrate for my tables in the MS SQL database. What else do I need to write in the model? If simple: use tables from the MS SQL server's database, but I also want to solve problem with migrations, etc.

推荐答案

如果您需要在现有数据库之上运行 Rails,并且希望确保可以创建与现有表匹配的所有模型,请遵循本指南:

If you need to run your Rails up on top of the existing database and you want to make sure you can create all models that match existing tables, follow this guide:

  • 使用 http://guides 将架构转储到 schema.rb.rubyonrails.org/migrations.html#schema-dumping-and-you.我实际上会使用模式的 SQL 模式 (config.active_record.schema_format = :sql ),并且只使用 SQL Server 工具生成数据库模式的 SQL 文件并将其保存为 db/structure.sql
  • 建立数据库架构后,您可以使用此处概述的方法重定向模型中的表和主键:将 Rails 置于现有数据库之上
  • Dump the schema into schema.rb using http://guides.rubyonrails.org/migrations.html#schema-dumping-and-you. I would actually use SQL mode of schema (config.active_record.schema_format = :sql ) and just use SQL Server tools to generate SQL file of the database schema and preserve it as db/structure.sql
  • After the database schema has been established, you can redirect tables and primary keys in your models using an approach outlined here: Putting Rails over top of an existing database

作为 ActiveRecord 的替代品,您可以考虑使用 DataMapper,因为它声称与 "棕地"(即已经建立的)数据库.

As an alternative to ActiveRecord altogether, you might consider DataMapper as it claims to work better with "brownfield" (i.e. already established) databases.

相关文章