简体   繁体   English

用Rails设置sql_mode

[英]Setting sql_mode with Rails

I'm deploying my rails application on a shared host, using mysql. 我正在使用mysql在共享主机上部署Rails应用程序。 During development, however, I'm using an sqlite database. 但是,在开发过程中,我正在使用sqlite数据库。 My queries need to work on both these environments. 我的查询需要在这两种环境下工作。 I'd earlier asked about a query using concat that'd work on both databases: Can MySQL concatenate strings with || 之前我曾问过使用concat在两个数据库上都可以使用的查询: MySQL可以用||来连接字符串吗?

I was told that I should set sql_mode to PIPES_AS_CONCAT or ANSI. 有人告诉我应该将sql_mode设置为PIPES_AS_CONCAT或ANSI。 Now my question is where and how should I set sql_mode in my application. 现在我的问题是我应该在哪里以及如何在应用程序中设置sql_mode。 Can it be done as an initializer, so as not to change my general code? 可以将其作为初始化程序来完成,以免更改我的通用代码吗?

I tried using the code snippet from here: http://gabrito.com/post/configuring-mysql-sql-mode-in-ruby-on-rails , but that didn't work - I'm guessing the connect method has since changed as that post is rather old. 我尝试从此处使用代码段: http : //gabrito.com/post/configuring-mysql-sql-mode-in-ruby-on-rails ,但这没有用-我猜connect方法有由于该职位已过时,因此已更改。

Any help is much appreciated. 任何帮助深表感谢。 I'm using rails 3.1.3, by the way. 顺便说一下,我正在使用Rails 3.1.3。

The main difference is probably that you're probably using the mysql2 adapter - try overwriting that method on ActiveRecord::ConnectionAdapters::Mysql2Adapter instead. 主要区别可能是您可能正在使用mysql2适配器-尝试改用ActiveRecord::ConnectionAdapters::Mysql2Adapter上的该方法。

Personally I think using different databases in development and production is asking for trouble (unless perhaps you're writing something designed to be able to run on many different databases). 我个人认为在开发和生产中使用不同的数据库会带来麻烦(除非您正在编写旨在能够在许多不同的数据库上运行的东西)。 The differences between databases can be subtle, for example sqlite3 has a rather flexible view when it comes to column types - you can quite happily insert > 255 characters in a column declared as VARCHAR(255) , whereas mysql would truncate the data. 数据库之间的差异可能非常细微,例如,sqlite3在列类型方面具有相当灵活的视图-您可以很高兴地在声明为VARCHAR(255)的列中插入> 255个字符,而mysql会截断数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM