Problems with ActiveRecord after certain amount of columns in table
I first had this problem like a week ago and I guess the fact wasn't very well explained mainly because I couldn't isolate the problem...apparently now I have...but still don't know its nature
I have a migration that goes like:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.references "faculty"
t.references "department"
t.references "role", :null => false
t.string "name", :null => false, :limit => 20
t.string "surname", :null => false, :limit => 20
t.string "username",:null => false, :limit => 25
t.string "study_group",:limit => 6
t.string "study_course",:limit => 50
t.string "card_code",:limit => 12
t.boolean "During_Day",:default => false
t.string "email", :limit => 100
t.string "hashed_password", :limit => 40
t.string "salt", :limit => 40
end
end
def self.down
drop_table :users
end
end
If I start to create users from the rails console and save them when trying to recover the data through
User.all
some fields' info would be corrupted.I started to remove fields and it works when I leave the first 8 columns (including the ID which is created by default).Creating a ninth column or even more would damage everything and afterwards I'm getting all kind of not correct information in my views. I must say that when from the rails console I save something and then check it from the mysql command line then the info is fine....so what's ActiveRecord missing?
output for:
Could you run
show create table users\G
from mysql console?
mysql> show create table users\G
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`faculty_id` int(11) default NULL,
`department_id` int(11) default NULL,
`role_id` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`surname` varchar(20) NOT NULL,
`username` varchar(25) NOT NULL,
`study_group` varchar(6) default NULL,
`study_course` varchar(50) default NULL,
`card_code` varchar(15) default NULL,
`day_time` tinyint(1) default '0',
`email` varchar(100) default NULL,
`hashed_password` varchar(40) default NULL,
`salt` varchar(40) default NULL,
PRIMARY KEY (`id`),
KEY `faculty` (`faculty_id`),
KEY `department` (`department_id`),
KEY `role` (`role_id`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.02 sec)
mysql>
adding user from the rails console
Loading development environment (Rails 3.0.3)
irb(main):001:0> me = User.new
=> #<User id: nil, faculty_id: nil, department_id: nil, role_id: nil,
name: "", surname: "", username: "", study_group: nil, study_cour
se: nil, card_code: nil, day_time: false, email: nil, hashed_password:
nil, salt: nil>
irb(main):002:0> me.name = 'Daniel'
=> "Daniel"
irb(main):003:0> me.surname = 'Garcia'
=> "Garcia"
irb(main):004:0> me.username = 'vinagrito'
=> "vinagrito"
irb(main):005:0> me.role_id = 1
=> 1
irb(main):006:0> me.save
=> false
irb(main):007:0> me.errors
=> {:password=>["is too short (minimum is 8 characters)"], :email=>["is
invalid", "can't be blank"]}
irb(main):008:0> me.password = '12345678'
=> "12345678"
irb(main):009:0> me.email = 'mail@mail.com'
=> "mail@mail.com"
irb(main):010:0> me.save
=> true
irb(main):011:0> me
=> #<User id: 2, faculty_id: nil, department_id: nil, role_id: 1, name:
"Daniel", surname: "Garcia", username: "vinagrito", study_group
: nil, study_course: nil, card_code: nil, day_time: false, email:
"mail@mail.com", hashed_password: "6305ee7016b263c0ec41a81439a378837a
318035", salt: "5c33e8fed10b87c9f4b7841f0faeb7b10424289a">
irb(main):013:0> user = User.where(:id => 2)
=> [#<User id: 2, faculty_id: nil, department_id: nil, role_id: 1, name:
"Daniel", surname: "Garcia", username: "vinagrito", study_grou
p: nil, study_course: nil, card_code: nil, day_time: false, email:
"mail@mail.com", hashed_password: 6305.0, salt: "5c33e8fed10b87c9f4b
7841f0faeb7b10424289a">]
See the "hashed_password" it returns to me
Output of the development.log
[1m[36mSQL (1.0ms)[0m [1mSHOW TABLES[0m
[1m[35mSQL (2.0ms)[0m SHOW TABLES
[1m[36mSQL (1.0ms)[0m [1mSELECT `schema_migrations`.`version` FROM
`schema_migrations`[0m
[1m[35mSQL (31.0ms)[0m CREATE TABLE `users` (`id` int(11) DEFAULT
NULL auto_increment PRIMARY KEY, `faculty_id` int(11), `department_id`
int(11), `role_id` int(11) NOT NULL, `name` varchar(20) NOT NULL,
`surname` varchar(20) NOT NULL, `username` varchar(25) NOT NULL,
`study_group` varchar(6), `study_course` varchar(50), `card_code`
varchar(15), `day_time` tinyint(1) DEFAULT 0, `email` varchar(100),
`hashed_password` varchar(40), `salt` varchar(40)) ENGINE=InnoDB
[1m[36mSQL (68.0ms)[0m [1mCREATE INDEX faculty ON
users(faculty_id)[0m
[1m[35mSQL (65.0ms)[0m CREATE INDEX department ON
users(department_id)
[1m[36mSQL (78.0ms)[0m [1mCREATE INDEX role ON users(role_id)[0m
[1m[35mSQL (80.8m开发者_开发技巧s)[0m CREATE INDEX username ON users(username)
[1m[36mSQL (30.0ms)[0m [1mINSERT INTO `schema_migrations` (`version`)
VALUES ('20101226144503')[0m
[1m[35mSQL (7.0ms)[0m SHOW TABLES
[1m[36mSQL (3.0ms)[0m [1mSELECT `schema_migrations`.`version` FROM
`schema_migrations`[0m
[1m[35mSQL (2.0ms)[0m SHOW TABLES
[1m[36mSQL (7.0ms)[0m [1mdescribe `authors`[0m
[1m[35mSQL (2.0ms)[0m SHOW KEYS FROM `authors`
[1m[36mSQL (7.0ms)[0m [1mdescribe `book_loans`[0m
[1m[35mSQL (3.0ms)[0m SHOW KEYS FROM `book_loans`
[1m[36mSQL (7.0ms)[0m [1mdescribe `books`[0m
[1m[35mSQL (2.0ms)[0m SHOW KEYS FROM `books`
[1m[36mSQL (7.0ms)[0m [1mdescribe `departments`[0m
[1m[35mSQL (3.0ms)[0m SHOW KEYS FROM `departments`
[1m[36mSQL (8.0ms)[0m [1mdescribe `faculties`[0m
[1m[35mSQL (3.0ms)[0m SHOW KEYS FROM `faculties`
[1m[36mSQL (7.0ms)[0m [1mdescribe `roles`[0m
[1m[35mSQL (2.0ms)[0m SHOW KEYS FROM `roles`
[1m[36mSQL (7.0ms)[0m [1mdescribe `subjects`[0m
[1m[35mSQL (2.0ms)[0m SHOW KEYS FROM `subjects`
[1m[36mSQL (7.0ms)[0m [1mdescribe `users`[0m
[1m[35mSQL (1.0ms)[0m SHOW KEYS FROM `users`
[1m[36mSQL (0.0ms)[0m [1mSHOW TABLES[0m
[1m[35mSQL (0.0ms)[0m BEGIN
[1m[36mSQL (0.0ms)[0m [1mdescribe `users`[0m
[1m[35mAREL (0.0ms)[0m INSERT INTO `users` (`faculty_id`,
`department_id`, `role_id`, `name`, `surname`, `username`,
`study_group`, `study_course`, `card_code`, `day_time`, `email`,
`hashed_password`, `salt`) VALUES (NULL, NULL, 1, 'Daniel', 'Garcia',
'viangrito', NULL, NULL, NULL, 0, 'mail@mail.com',
'e1382ac8675daee167a262ee7ef3bd038f997c4a',
'a3518e0a6b273590073bd733e2ee692fbca28c2d')
[1m[36mSQL (15.6ms)[0m [1mCOMMIT[0m
So finally after a couple of days of struggling, yesterday I found
https://github.com/brianmario/mysql2/issues#issue/71
Which was found trying to locate someone else with similar problems I don't know how it was for other windows users but my schema.rb was showing me no schema at all just:
ActiveRecord::Schema.define(:version => XXXXXXXXXXXXXX) do #my last migration version
#Could not dump table "table_name" beacuse of following ArgumentError
# invalid date
.
.
.# and so one for each table I had
end
So I downloaded mysql 5.1. from http://dev.mysql.com/downloads/mysql/5.1.html and now everything's working like it should. Not it shows the full schema of my db and when retrieving data from it no longer gets corrupted
精彩评论