Rails Active Record - 带引号的标识符 - MS Sql Server 保留的表名称
我有一个 SQL Server 2005 旧数据库,其中有一个名为 user 的表和一个映射到“用户”表的模型用户。
“user”是Sql Server中的保留字,因此我需要ActiveRecord在查询中引用/括号表名,因为
Select u.* from [user] where [user].[Id] = 1
默认情况
Select u.* from user where user.[Id] = 1
下由于保留字冲突而失败。
如何通知 Rails 使用标准带引号的标识符或括号标识符来分隔表名称中的保留字和/或空格?
导轨模型
class User < UserBase
set_primary_key "Id"
set_table_name "user"
end
class UserBase < ActiveRecord::Base
establish_connection "tums_#{[Rails.env]}"
self.abstract_class = true
end
I have a SQL Server 2005 legacy database which has a table named user and a model User which maps to the 'user' table.
'user' is a reserved word in Sql Server, so I need ActiveRecord to quote/bracket the table name in queries as
Select u.* from [user] where [user].[Id] = 1
the default is
Select u.* from user where user.[Id] = 1
which fails due to the reserve word conflict.
How do I inform rails to use use either the standard quoted identifiers or bracketed identifiers to delimit reserved words and/or spaces in table names?
Rails Models
class User < UserBase
set_primary_key "Id"
set_table_name "user"
end
class UserBase < ActiveRecord::Base
establish_connection "tums_#{[Rails.env]}"
self.abstract_class = true
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 MS Sql 服务器的 jruby activerecord jdbc 适配器源中的错误。此后该问题已得到修复。
https://github.com/jruby/activerecord-jdbc-adapter/issues/49
This was a bug in the jruby activerecord jdbc adapter source for MS Sql server. It has since been fixed.
https://github.com/jruby/activerecord-jdbc-adapter/issues/49