我正在关注此Ruby on Rails tutorial from TutorialsPoint.com。我对Ruby on Rails开发完全陌生,并且已经遇到了一些小问题。
我正在使用Ruby 2.3.3p222(2016-11-21修订版56859)[i386-mingw32]和Ruby on Rails 5.1.5。
出于某种原因,在使用指定的代码添加list.html.erb并尝试运行" rails server"后,在打开http://localhost:3000时,我收到此迁移错误:
当我运行" rails db:migrate RAILS_ENV = development"时,我在控制台中收到了这个错误日志:
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)
我在db / migrate目录中有这些文件(我不知道为什么即使我开始创建项目,它为Books表创建了一对迁移文件,为Subjects表创建了另一个): / p>
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
这真的很奇怪,因为我不认为我应该在迁移文件之间有重复的代码。当我现在只创建数据库并将其用作整个教程时,我甚至看不到使用self.up和self.down函数的重要性。
我该怎么做才能解决这个问题?我想我需要摆脱self.up和self.down函数,再次运行rake db:migrate,但我不知道是否需要额外的步骤或其他方式。
答案 0 :(得分:1)
我不确定教程的来源,但最后两个迁移文件看起来可能与以前的版本相同。
尝试删除它们并重新启动Rails服务器,看看是否修复了它。
如果没有通过运行 <system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService1.IService1"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="false" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<!--<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
重置数据库(这相当于删除数据库然后重新创建它。数据库中的任何数据都将丢失,但我不认为这应该是你的问题)。
答案 1 :(得分:0)
似乎您的/db/migrate
文件夹中有相同的迁移文件。我的意思是schema.rb
文件中存在的用于创建表的相同文件。因此,您只需从/db/migrate
文件夹中删除这些文件,然后尝试再次运行rake db:migrate
命令。
答案 2 :(得分:0)
我遇到了这个问题。我使用了“ .Env”文件来存储我的PG凭据,如果是这种情况,请确保您的Gem文件中包含该gem。
gem 'dotenv-rails'