activerecord create_table 像现有表一样
使用 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 table
new像现有的
语句?
除了显然 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 table
newlike existing
statement?
Besides obviously connection.execute(string)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有。括号被硬编码在
create_table
中:对字符串文字使用execute没有任何问题;如果您不想编写快速补丁,我会这样做。
Nope. The parenthesis are hard coded in
create_table
: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.