Rails 4 Migration: Mysql2::Error: Data too long for column 'xxxx' -
here schema.rb
create_table "users", force: true |t| t.string "name", limit: 6 t.string "email" t.datetime "created_at" t.datetime "updated_at" end i set limit fo string column "name".
then, in console:
user = user.new(name:"1234567890",email:"username@gmail.com") user.save! it raised error:
activerecord::statementinvalid: mysql2::error: info long column 'name' @ row 1: insert `users` (`created_at`, `email`, `name`, `updated_at`) values ('2014-06-19 15:08:15', 'username@gmail.com', '1234567890', '2014-06-19 15:08:15') but, when switched rails 3.
i found truncated string "1234567890" automatically, , inserted "123456" database without error.
is there has been removed in rails 4?
should add together truncate functions in model myself? thanks!
what you're seeing difference in mysql, not rails. default, mysql truncate info that's long rather throwing error. if set mysql strict mode, throw errors instead of silently truncating data.
with version 4, rails turns on strict mode default. that's why you're seeing different behavior rails 3. is behavior want. silently truncating info bad , can lead confusing behavior users.
if really want truncate data, turn off strict mode or utilize before filter:
before_save :truncate_username def truncate_username self.username = username.slice(0, 6) end ruby-on-rails ruby-on-rails-4 rails-migrations
No comments:
Post a Comment