在MySQL中使用rake db:migrate命令在Rails中创建表的主键问题 [英] PRIMARY KEY issue with creating tables in Rails using rake db:migrate command with mysql

查看:265
本文介绍了在MySQL中使用rake db:migrate命令在Rails中创建表的主键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的rails版本是4.0.0,我的mysql版本是Ver 14.14 Distrib 5.7.9,适用于Win64(x86_64).我正在使用旧版本的Rails,因为按照我之前的问题 Kalelc 为我的追索权获得批准的答案)

My version of rails is 4.0.0, my version of mysql is Ver 14.14 Distrib 5.7.9, for Win64 (x86_64). I am operating of an older version of rails as I was getting some clashes with the mysql as per my previous question Here. (check Kalelc's approved answer for my recourse)

跑步时

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,这是代码

I do not set any values to NULL in my code, here is the code

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

此代码完全,如我所关注的教程中所示.我还研究了其他此处类似的问题溢出并遵循给出的建议.我已经尝试过建议的猴子补丁

This code is exactly as shown in the tutorials I am following. I have also investigated other similar issues here on stack overflow and have followed the advice given. I have tried the monkey patch as suggested

# lib/patches/abastract_mysql_adapter.rb
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

我将此文件插入到我在simple_cms应用程序的lib中创建的名为patch的文件夹中.我已将文件另存为"abstract_mysql_adapter.rb" 就像在同一只猴子补丁中所建议的那样. 我已使用以下

I inserted this file into a folder I have created called patches inside the lib of my simple_cms application. I have saved down the file as "abstract_mysql_adapter.rb" as suggested in the same monkey patch. I have updated my environment.rb of the simple_cms application with the following

require File.expand_path('../../lib/patches/abstract_mysql_adapter.rb', __FILE__)

如果我随后运行rake db:migrate命令

if I then run a rake db:migrate command

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绝对是猴子补丁的路径.我是否将补丁放置在错误的位置?我在这里做错了什么,挠了一下这个头?抱歉,这对于某些人来说是显而易见的,但是在禁欲期很长之后,我将重新编码,而我无法解决这个问题.在此先感谢您的帮助:)

C:/Users/Lizanne/Documents/Code/Sites/simple_cms/lib/patches/abstract_mysql_adapter.rb is most definitely the path to the monkey patch. Have I put the patch in the wrong place? What am I doing wrong here, scratching my head over this one? Apologies if this is obvious to some but I'm returning to coding after a very long abstinence and I cant get my head around the issue. Many thanks in advance for you help :)

推荐答案

我最近也遇到了同样的问题.

I too recently faced same issue.

MySQL 5.7不再支持主键的空默认值.

MySQL 5.7 no longer supports null default values for the primary key.

通过覆盖MySql中主键的本机默认值,可以解决您的问题.

By overriding the Native default for primary keys in MySql you can resolve your issue.

在config/initializers/abstract_mysql_adapter.rb中:

In config/initializers/abstract_mysql_adapter.rb:

class ActiveRecord::ConnectionAdapters::MysqlAdapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

对于mysql2,它应该是config/initializers/abstract_mysql2_adapter.rb:

For mysql2 it should be config/initializers/abstract_mysql2_adapter.rb:

class ActiveRecord::ConnectionAdapters::Mysql2Adapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

这篇关于在MySQL中使用rake db:migrate命令在Rails中创建表的主键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆