所以我在lynda.com上关注Ruby on Rails 4 Essential Training。我已经完成了第6章“生成迁移”。
我的问题是当我运行rake db:migrate
时,我得到:
== 20160918162646 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `first_name` varchar(25), `last_name` varchar(50), `email` varchar(255) DEFAULT ' ' NOT NULL, `password` varchar(40), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
/Users/bryanbean/Sites/simple_cms/db/migrate/20160918162646_create_users.rb:4:in `up'
ActiveRecord::StatementInvalid: Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `first_name` varchar(25), `last_name` varchar(50), `email` varchar(255) DEFAULT ' ' NOT NULL, `password` varchar(40), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
/Users/bryanbean/Sites/simple_cms/db/migrate/20160918162646_create_users.rb:4:in `up'
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead
/Users/bryanbean/Sites/simple_cms/db/migrate/20160918162646_create_users.rb:4:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
为什么我的数据库不能迁移?
迁移文件:
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
# t.datetime "created_at"
# t.datetime "updated_at"
t.timestamps
end
end
def down
drop_table :users
end
end