ruby-on-rails – Mysql2 ::错误:PRIMARY KEY的所有部分必须为NOT NULL;如果键中需要NULL,请改用UNIQUE

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Mysql2 ::错误:PRIMARY KEY的所有部分必须为NOT NULL;如果键中需要NULL,请改用UNIQUE前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在rails 3.2.9和 ruby versiion 2.0.0中创建了一个演示应用程序.脚手架Blog模型后我试图迁移它,但有以下问题.

# rake db:migrate

== CreateBlogs: migrating ====================================================

— create_table(:blogs)
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 blogs (id int(11) DEFAULT NULL auto_increment PRIMARY KEY,title varchar(255),description text,created_at datetime NOT NULL,updated_at datetime NOT NULL) ENGINE=InnoDB
/usr/local/rvm/gems/ruby-2.0.0-p648@demo-app/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in query'
/usr/local/rvm/gems/ruby-2.0.0-p648@demo-app/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract_MysqL_adapter.rb:245:in
block in execute’
/usr/local/rvm/gems/ruby-2.0.0-p648@demo-app/gems/activerecord-

….

….

….

-3.2.9/lib/active_record/migration.rb:551:in migrate'
/usr/local/rvm/gems/ruby-2.0.0-p648@demo-app/gems/activerecord-3.2.9/lib/active_record/railties/databases.rake:179:in
block (2 levels) in ‘
/usr/local/rvm/gems/ruby-2.0.0-p648@demo-app/gems/rake-11.3.0/exe/rake:27:in `’
Tasks: TOP => db:migrate
(See full trace by running task with –trace)

解决方法

配置/初始化/ abstract_MysqL2_adapter.rb
require 'active_record/connection_adapters/MysqL2_adapter'

class ActiveRecord::ConnectionAdapters::MysqL2Adapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
原文链接:https://www.f2er.com/ruby/267670.html

猜你在找的Ruby相关文章