Ruby Rails 5.1.5 - 遵循 TutorialsPoint 教程时出现迁移错误(关系已存在)
Ruby on Rails 5.1.5 - Migration Error (Relation already exists) in following TutorialsPoint tutorial
我正在关注这个 Ruby on Rails tutorial from TutorialsPoint.com。我对 Ruby 的 Rails 开发完全陌生,并且已经 运行 遇到了一些小问题。
我正在使用 Ruby 2.3.3p222(2016-11-21 修订版 56859)[i386-mingw32] 和 Ruby 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 table 创建了一对迁移文件,另一个为主题 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
这真的很奇怪,因为我不认为我应该在迁移文件之间有重复的代码。当我现在只是创建数据库并将其用于整个教程时,我什至没有看到使用 self.up 和 self.down 函数的重要性。
我应该怎么做才能解决这个问题?我想我需要摆脱 self.up 和 self.down 功能,并再次 运行 rake db:migrate,但我不知道是否需要额外的步骤或其他方式。
我不确定教程的来源,但最后两个迁移文件看起来可能来自以前的版本。
尝试删除它们并重新启动 Rails 服务器,看看是否可以解决问题。
如果不通过 运行 rails db:reset
重置数据库(这相当于删除数据库然后重新创建它。数据库中的任何数据都会丢失,但我不认为对你来说应该是个问题)。
您的 /db/migrate
文件夹中似乎有相同的迁移文件。我的意思是用于创建表的相同文件存在于您的 schema.rb
文件中。因此,您只需从 /db/migrate
文件夹中删除这些文件,然后再次尝试 运行 rake db:migrate
命令。
我有这个问题。我使用“.Env”文件来存储我的 PG 凭据,如果是这样,请确保你的 Gemfile 中有这个 gem。
gem 'dotenv-rails'
我正在关注这个 Ruby on Rails tutorial from TutorialsPoint.com。我对 Ruby 的 Rails 开发完全陌生,并且已经 运行 遇到了一些小问题。
我正在使用 Ruby 2.3.3p222(2016-11-21 修订版 56859)[i386-mingw32] 和 Ruby 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 table 创建了一对迁移文件,另一个为主题 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
这真的很奇怪,因为我不认为我应该在迁移文件之间有重复的代码。当我现在只是创建数据库并将其用于整个教程时,我什至没有看到使用 self.up 和 self.down 函数的重要性。
我应该怎么做才能解决这个问题?我想我需要摆脱 self.up 和 self.down 功能,并再次 运行 rake db:migrate,但我不知道是否需要额外的步骤或其他方式。
我不确定教程的来源,但最后两个迁移文件看起来可能来自以前的版本。
尝试删除它们并重新启动 Rails 服务器,看看是否可以解决问题。
如果不通过 运行 rails db:reset
重置数据库(这相当于删除数据库然后重新创建它。数据库中的任何数据都会丢失,但我不认为对你来说应该是个问题)。
您的 /db/migrate
文件夹中似乎有相同的迁移文件。我的意思是用于创建表的相同文件存在于您的 schema.rb
文件中。因此,您只需从 /db/migrate
文件夹中删除这些文件,然后再次尝试 运行 rake db:migrate
命令。
我有这个问题。我使用“.Env”文件来存储我的 PG 凭据,如果是这样,请确保你的 Gemfile 中有这个 gem。
gem 'dotenv-rails'