Mysql2::Error: Incorrect string value

后端 未结 7 1480
青春惊慌失措
青春惊慌失措 2021-01-29 23:30

I have a rails application running on production mode, but all of the sudden this error came up today when a user tried to save a record.



        
7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 00:12

    If you want to the store emoji, you need to do the following:

    1. Create a migration (thanks @mfazekas)

      class ConvertTablesToUtf8 < ActiveRecord::Migration def change_encoding(encoding,collation) connection = ActiveRecord::Base.connection tables = connection.tables dbname =connection.current_database execute <<-SQL ALTER DATABASE #{dbname} CHARACTER SET #{encoding} COLLATE #{collation}; SQL tables.each do |tablename| execute <<-SQL ALTER TABLE #{dbname}.#{tablename} CONVERT TO CHARACTER SET #{encoding} COLLATE #{collation}; SQL end end

      def change reversible do |dir| dir.up do change_encoding('utf8mb4','utf8mb4_bin') end dir.down do change_encoding('latin1','latin1_swedish_ci') end end end end

    2. Change rails charset to utf8mb4 (thanks @selvamani-p)

      production: encoding: utf8mb4

    References:

    https://stackoverflow.com/a/39465494/1058096

    https://stackoverflow.com/a/26273185/1058096

提交回复
热议问题