- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在关注这个 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 表创建了另一个):
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
重置数据库(这相当于删除数据库然后重新创建它。数据库中的任何数据都将丢失,但我不认为对你来说应该是个问题)。
关于ruby-on-rails - Ruby on Rails 5.1.5 - 以下 TutorialsPoint 教程中的迁移错误(关系已存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48897945/
http://www.tutorialspoint.com/design_pattern/proxy_pattern.htm 嗨, 我希望使用示例中的示例来了解java中的代理设计模式上面的链接。在主
#include #include const int MAX = 3; int main() { int var[] = { 10, 100, 200 }; int i, *pt
TutorialsPoint Java Compiler 在 tutorialspoint 中,他们使用 term.js 创建了 linux 终端. 我在我的项目中集成了相同的 github 库,它工
我是 Java 和 REST API 的新手,我遇到了以下问题。我点击那个链接 https://www.tutorialspoint.com/restful/restful_first_applica
我正在关注这个 Ruby on Rails tutorial from TutorialsPoint.com .我对 Ruby on Rails 开发完全陌生,并且已经遇到了一些小问题。 我正在使用
我是 Node.js 的初学者,并尝试使用 express.js。我按照 Tutorialpoint 的教程来学习express.js。但是,我在 node.js Restful api sessio
我是一名优秀的程序员,十分优秀!