获取PG:UndefinedTable:错误:运行迁移时添加友谊表(开发中)

时间:2017-03-11 21:29:42

标签: ruby-on-rails ruby postgresql associations rails-activerecord

我正在尝试构建一个拥有用户和友谊的应用。我正在使用Rails 5,Ruby 2.4.0和PostgreSQL 9.5.6

我目前正在尝试实施添加好友功能。我为友谊和朋友添加了has_many关联(通过友谊,使用User类)。

我发现类似的错误消息与没有创建命名表有关,但在我的情况下,表朋友应该只是我的用户表,别名为朋友。

当我尝试运行迁移以生成我的友谊表时,我收到以下错误

-- create_table(:friendships)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "friends" does not exist
: CREATE TABLE "friendships" ("id" serial primary key, "user_id" integer, "friend_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_e3733b59b7"
FOREIGN KEY ("user_id")
  REFERENCES "users" ("id")
, CONSTRAINT "fk_rails_d78dc9c7fd"
FOREIGN KEY ("friend_id")
  REFERENCES "friends" ("id")
)
/home/disc0ninja/Documents/Udemy/rails/workout-app/db/migrate/20170311194725_create_friendships.rb:3:in `change'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `require'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `<top (required)>'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "friends" does not exist
: CREATE TABLE "friendships" ("id" serial primary key, "user_id" integer, "friend_id" integer, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_e3733b59b7"
FOREIGN KEY ("user_id")
  REFERENCES "users" ("id")
, CONSTRAINT "fk_rails_d78dc9c7fd"
FOREIGN KEY ("friend_id")
  REFERENCES "friends" ("id")
)
/home/disc0ninja/Documents/Udemy/rails/workout-app/db/migrate/20170311194725_create_friendships.rb:3:in `change'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `require'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `<top (required)>'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
PG::UndefinedTable: ERROR:  relation "friends" does not exist
/home/disc0ninja/Documents/Udemy/rails/workout-app/db/migrate/20170311194725_create_friendships.rb:3:in `change'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `require'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/rails:9:in `<top (required)>'
/home/disc0ninja/Documents/Udemy/rails/workout-app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

通过以下方式生成友谊模型时创建的迁移:

rails g model friendship user:references friend:references

以下是迁移文件

class CreateFriendships < ActiveRecord::Migration[5.0]
  def change
    create_table :friendships do |t|
      t.references :user, foreign_key: true
      t.references :friend, foreign_key: true

      t.timestamps
    end
  end
end

这是我的用户模型:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :friendships
  has_many :friends, through: :friendships, class_name: 'User'

  validates :first_name, presence: true
  validates :last_name, presence: true
end

在以某种方式运行之前,我是否需要通过迁移中的友谊为朋友添加关联?

编辑:添加了迁移文件

1 个答案:

答案 0 :(得分:2)

将迁移更改为使用index而不是foreign_key似乎已停止了错误消息,并允许我使用我之后的功能。

class CreateFriendships < ActiveRecord::Migration[5.0]
  def change
    create_table :friendships do |t|
      t.references :user, foreign_key: true
      t.references :friend, index: true

      t.timestamps
    end
  end
end