How to turn off MySQL strict mode in Rails

孤街醉人 提交于 2019-11-28 21:16:16

You can set strict mode in your database.yml using strict: false as follows:

production:
  host: ...
  username: ...
  strict: false

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/MysqlAdapter.html

The mysql2 gem exposes an option to execute an initial command on connect and reconnect. You can set the init_command from inside database.yml:

production:
  host: ...
  username: ...
  init_command: "SET @@SESSION.sql_mode = ''"

You can add this to your database.yml

variables:
   sql_mode: 'traditional'

or

variables:
   strict_mode: false

See:

https://github.com/rails/rails/pull/8346

Following @edubriguenti's answer, I found this. Applying the post and the comment there, I added the following to environment.rb and it looks to have solved the problem.

# Set MySQL to clear sql mode for all connections
class ActiveRecord::ConnectionAdapters::Mysql2Adapter 
  alias :connect_no_sql_mode :connect
  def connect
    connect_no_sql_mode
    execute("SET sql_mode = ''")
  end
end

ActiveRecord::Base.connection.reconnect!

@arya 's answer might not work because 'traditional' sql mode is strict, if you want to set it to non-strict, try this:

sql_mode: ''

Hope that helps

Try this:

# Set MySQL to clear sql mode for all connections
class ActiveRecord::ConnectionAdapters::MysqlAdapter 
  alias :connect_no_sql_mode :connect
  def connect
    connect_no_sql_mode
    execute("SET sql_mode = ''")
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!