jeudi 9 juin 2022

Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL

I am using a legacy version of rails 3.2.22 and while trying to execute my migration file, I encountered this error:

rake db:migrate
==  CreateBooks: migrating ====================================================
-- create_table(:books)
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 `books` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `Book Title` varchar(255), `Author` varchar(255), `Date Published` date, `Book ID` int(11), `Checked Out?` tinyint(1), `Return Time` datetime, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB
/usr/share/rvm/gems/ruby-2.1.10/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
/usr/share/rvm/gems/ruby-2.1.10/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `block in query'
/usr/share/rvm/gems/ruby-2.1.10/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `handle_interrupt'
/usr/share/rvm/gems/ruby-2.1.10/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `query'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/mysql2_adapter.rb:213:in `execute'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:434:in `create_table'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:466:in `block in method_missing'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `block in say_with_time'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `say_with_time'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:458:in `method_missing'
/home/elliot/Desktop/SAIT WORK/Library/db/migrate/20220609183208_create_books.rb:3:in `up'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:410:in `block in migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:389:in `migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:528:in `migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:777:in `call'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:777:in `ddl_transaction'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:719:in `block in migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `each'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:700:in `migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:570:in `up'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/migration.rb:551:in `migrate'
/usr/share/rvm/gems/ruby-2.1.10/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:193:in `block (2 levels) in <top (required)>'
/usr/share/rvm/gems/ruby-2.1.10/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
/usr/share/rvm/gems/ruby-2.1.10/bin/ruby_executable_hooks:24:in `eval'
/usr/share/rvm/gems/ruby-2.1.10/bin/ruby_executable_hooks:24:in `<main>'

I am well aware of the fact that people have posted this problem in the past and there have been solutions posted that helped solve this problem but when I attempt to utilize those solutions, I encounter errors. In particular, I am referencing this guide: Link to solution 1

However, when I implement the suggested changes, I can run the rake db:migrate, however, when I try to run any other rails command (i.e. rails s or rails console), I get this error:

rails s
=> Booting WEBrick
=> Rails 3.2.22 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- /home/elliot/Desktop/SAIT WORK/Library/lib/patches/abstract_mysql_adapter (LoadError)
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    from /home/elliot/Desktop/SAIT WORK/Library/config/environment.rb:3:in `<top (required)>'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    from /home/elliot/Desktop/SAIT WORK/Library/config.ru:3:in `block in <main>'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/builder.rb:51:in `instance_eval'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/builder.rb:51:in `initialize'
    from /home/elliot/Desktop/SAIT WORK/Library/config.ru:in `new'
    from /home/elliot/Desktop/SAIT WORK/Library/config.ru:in `<main>'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/builder.rb:40:in `eval'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/builder.rb:40:in `parse_file'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/server.rb:200:in `app'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/railties-3.2.22/lib/rails/commands/server.rb:46:in `app'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/server.rb:304:in `wrapped_app'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/rack-1.4.7/lib/rack/server.rb:254:in `start'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/railties-3.2.22/lib/rails/commands/server.rb:70:in `start'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/railties-3.2.22/lib/rails/commands.rb:55:in `block in <top (required)>'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/railties-3.2.22/lib/rails/commands.rb:50:in `tap'
    from /usr/share/rvm/gems/ruby-2.1.10/gems/railties-3.2.22/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I have also tried the implementing the solution suggested here: Link to Solution 2 but I would run into the same error. Could this problem relate to the naming convention of my models and tables? I am very stuck so any help would be greatly appreciated.

app/models/book.rb

class Book < ActiveRecord::Base
  # attr_accessible :title, :body
end

config/database.yml !!!Note that I omitted the password and username for privacy, but they are implemented correctly I believe!!!

# MySQL.  Versions 4.1 and 5.0 are recommended.
# 
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: Library_development
  pool: 5
  username: **
  password: **
  socket: /var/run/mysqld/mysqld.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: Library_test
  pool: 5
  username: **
  password: **
  socket: /var/run/mysqld/mysqld.sock

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: Library_production
  pool: 5
  username: **
  password: **
  socket: /var/run/mysqld/mysqld.sock

db/migrate/20220609183208_create_books.rb

class CreateBooks < ActiveRecord::Migration
  def up
    create_table :books do |t|
      t.string 'Book Title'
      t.string 'Author'
      t.date "Date Published"
      t.integer "Book ID"
      t.boolean "Checked Out?"
      t.datetime "Return Time"
      t.timestamps
    end
  end

  def down
    drop_table :books
  end

end

Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.22'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

# gem 'mysql2'

gem 'mysql'
gem 'mysql2', "~> 0.3.10"

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

gem 'devise'

gem 'cancancan'

gem 'mail_interceptor', group: [:development, :staging]

#gem 'sidekiq', '~>6.0.0'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
  gem 'rspec-rails', '~> 3.5'
  gem 'factory_bot_rails'
  gem 'database_cleaner'
end

gem "bootstrap-sass", "~> 3.1.1.0"

Aucun commentaire:

Enregistrer un commentaire