在运行时,
rake db:migrate
我收到了以下错误信息
== 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=InnoDBC:/Users/Lizanne/Documents/Code/Sites/simple_cms/db/migrate/20151116154434_create_users.rb:3:in `up'
C:in `migrate'
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
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/db/migrate/20151116154434_create_users.rb:3:in `up'
C:in `migrate'
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/db/migrate/20151116154434_create_users.rb:3:in `up'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
我在我的代码中没有将任何值设置为NULL,以下是代码:
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.timestamps
end
end
def down
drop_table :users
end
end
这段代码与我正在跟随的教程完全相同。我还查看了其他类似问题在stack overflow上,并遵循了给出的建议。我已尝试使用monkey patch 如建议所示
# lib/patches/abastract_mysql_adapter.rb
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
我把这个文件插入到我创建的一个名为patches的文件夹中,该文件夹位于我的simple_cms应用程序的lib目录下。我按照同一monkey patch建议将文件保存为"abstract_mysql_adapter.rb"。 我已经使用以下内容更新了simple_cms应用程序的environment.rb
require File.expand_path('../../lib/patches/abstract_mysql_adapter.rb', __FILE__)
如果我接下来运行“rake db:migrate”命令。
rake aborted!
LoadError: cannot load such file -- C:/Users/Lizanne/Documents/Code/Sites/simple_cms/lib/patches/abstract_mysql_adapter.rb
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/config/environment.rb:3:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/lib/patches/abstract_mysql_adapter.rb是猴子补丁的路径。我把补丁放错了吗?我在这个问题上纠结了很久,不知道自己做错了什么。如果对一些人来说这很明显,那我向你们道歉,因为我已经很久没有写代码了,现在无法理解这个问题。非常感谢您的帮助 :)
"int(11) auto_increment PRIMARY KEY NOT NULL"
。 - Undolib/patches/abastract_mysql_adapter.rb
移动到config/initializers/abastract_mysql_adapter.rb
- 但如果你只是在学习Rails,为什么不使用sqlite呢?(我也更喜欢Postgres而不是MySQL,特别是如果你最终要在Heroku上托管你的应用程序) - house9bundle exec rake db:migrate
,但还是不行,出现了同样的错误。 - Aerosewall1