activerecord create_table 像现有表一样

发布于 2024-09-15 14:30:05 字数 664 浏览 8 评论 0原文

使用 Rails/ActiveRecord 2.3.8 我想做:

AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' )

但是 AR 坚持在生成的 SQL 中添加“()”,即使字段列表为空,因为表 DDL 正在被克隆,从而导致例如:

ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') like any_model' at line 1: 
CREATE TEMPORARY TABLE `temp_any_model` () like any_model

是否有任何如何强制 AR 生成这个简单的create tablenew像现有的语句?

除了显然 connection.execute(string)

With Rails/ActiveRecord 2.3.8 I'd like to do:

AnyModel.connection.create_table( 'temp_any_model', temporary: true, id: false, options: 'like any_model' )

But AR insists on adding "()" to the generated SQL even though the field list is blank since the table DDL is being cloned, thus resulting in e.g.:

ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') like any_model' at line 1: 
CREATE TEMPORARY TABLE `temp_any_model` () like any_model

Is there any way to coerce AR to generate this simple create tablenewlike existing statement?

Besides obviously connection.execute(string) ?

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

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

发布评论

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

评论(1

念﹏祤嫣 2024-09-22 14:30:05

没有。括号被硬编码在create_table中:

def create_table(table_name, options = {})
  # snipped ...

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "            
  create_sql << "#{quote_table_name(table_name)} ("                             
  create_sql << table_definition.to_sql                                         
  create_sql << ") #{options[:options]}"                                        
  execute create_sql        
end

对字符串文字使用execute没有任何问题;如果您不想编写快速补丁,我会这样做。

Nope. The parenthesis are hard coded in create_table:

def create_table(table_name, options = {})
  # snipped ...

  create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "            
  create_sql << "#{quote_table_name(table_name)} ("                             
  create_sql << table_definition.to_sql                                         
  create_sql << ") #{options[:options]}"                                        
  execute create_sql        
end

There's nothing wrong with using execute on a string literal; I would do that if you don't feel like writing a quick patch.

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