gpt4 book ai didi

ruby-on-rails - Ruby on Rails 5.1.5 - 以下 TutorialsPoint 教程中的迁移错误(关系已存在)

转载 作者:行者123 更新时间:2023-11-29 13:15:59 24 4
gpt4 key购买 nike

我正在关注这个 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 ,我收到此迁移错误:

PendingMigrationError

当我运行“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com