I am following this Ruby on Rails tutorial from TutorialsPoint.com. I'm completely new to Ruby on Rails development, and already running into some hiccups.
I am using Ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32] and Ruby on Rails 5.1.5.
For some reason, after adding list.html.erb with the code specified and trying to run "rails server", upon opening up http://localhost:3000, I get this migration error:
When I run "rails db:migrate RAILS_ENV=development", I get this log of errors in the console:
C:\Users\gregp\Documents\<secret directory>\Programming Experiments\TutorialsPointRoR\demo2>rails db:migrate RAILS_ENV=development
== 20180221025323 Books: migrating ============================================
-- create_table(:books)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "books" already exists
: CREATE TABLE "books" ("id" bigserial primary key, "title" character varying(32) NOT NULL, "price" float, "subject_id" integer, "description" text, "created_at" timestamp)
C:/Users/gregp/Documents/<secret directory>/Programming Experiments/TutorialsPointRoR/demo2/db/migrate/20180221025323_books.rb:3:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: relation "books" already exists
: CREATE TABLE "books" ("id" bigserial primary key, "title" character varying(32) NOT NULL, "price" float, "subject_id" integer, "description" text, "created_at" timestamp)
C:/Users/gregp/Documents/<secret directory>/Programming Experiments/TutorialsPointRoR/demo2/db/migrate/20180221025323_books.rb:3:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
PG::DuplicateTable: ERROR: relation "books" already exists
C:/Users/gregp/Documents/<secret directory>/Programming Experiments/TutorialsPointRoR/demo2/db/migrate/20180221025323_books.rb:3:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I have these files in the db/migrate directory (I don't know why even when I started creating the project, it created a pair of migrate files for the Books table and another for the Subjects table):
20180221024754_create_books.rb:
class CreateBooks < ActiveRecord::Migration[5.1]
def change
create_table :books do |t|
t.column :title, :string, :limit => 32, :null => false
t.column :price, :float
t.column :subject_id, :integer
t.column :description, :text
t.column :created_at, :timestamp
end
end
end
20180221024922_create_subjects.rb:
class CreateSubjects < ActiveRecord::Migration[5.1]
def change
create_table :subjects do |t|
t.column :name, :string
end
Subject.create :name => "Physics"
Subject.create :name => "Mathematics"
Subject.create :name => "Chemistry"
Subject.create :name => "Psychology"
Subject.create :name => "Geography"
end
end
20180221025323_books.rb:
class Books < ActiveRecord::Migration[5.1]
def self.up
create_table :books do |t|
t.column :title, :string, :limit => 32, :null => false
t.column :price, :float
t.column :subject_id, :integer
t.column :description, :text
t.column :created_at, :timestamp
end
end
def self.down
drop_table :books
end
end
20180221025434_subjects.rb:
class Subjects < ActiveRecord::Migration[5.1]
def self.up
create_table :subjects do |t|
t.column :name, :string
end
Subject.create :name => "Physics"
Subject.create :name => "Mathematics"
Subject.create :name => "Chemistry"
Subject.create :name => "Psychology"
Subject.create :name => "Geography"
end
def self.down
drop_table :subjects
end
end
This is really strange, because I don't think I am supposed to have duplicate code between the migrate files. I don't even see the significance of using the self.up and self.down functions when I'm only creating the database for now and using it for the tutorial as a whole.
What should I do to fix this problem? I think I need to get rid of the self.up and self.down functions, and run rake db:migrate again, but I don't know if there's an extra step needed or another way.
I'm not sure where the tutorial is from, but those last two migration files look like they might be there from a previous version.
Try deleting them and restart the Rails server and see if that fixes it.
If not reset the database by running rails db:reset
(that's the equivalent of dropping the database and then recreating it. Any data in the database will be lost, but I don't think that should be an issue for you).