ActiveRecord::StatementInvalid (Mysql::Error: Data too long for column 'title' at row 1: INSERT INTO `tasks` (`updated_at`, `is_arrange`, `title`, `client`, `content`, `user_id`, `is_over`, `created_at`) VALUES('2010-08-15 07:27:13', '', '二恶', NULL, '而非', NULL, '', '2010-08-15 07:27:13')):
app/controllers/task_controller.rb:53:in `create'
app/controllers/task_controller.rb:52:in `create'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
Rendered rescues/_trace (157.0ms)
Rendered rescues/_request_and_response (78.0ms)
Rendering rescues/layout (internal_server_error)
当出现时以上错误时,提示数据太长,一个原因是,字段的长度不够,另一个原因则是字符编码的问题
我遇到的这个情况是后一个原因,当输入中文时应该设置编码方式为utf8。
class CreateTasks < ActiveRecord::Migration
def self.up
create_table :tasks ,:options => "charset=utf8"
do |t|
t.string :title
t.integer :user_id
t.text :content
t.string :client
t.string :is_evaluate
t.string :is_over
t.string :is_arrange
t.timestamps
end
end
def self.down
drop_table :tasks
end
end
一定要把:options => "charset=utf8" 拼写对
当你写错的时候,迁移数据库时并不报错,所以这种问题很难找原因。