disc0ninja
disc0ninja

Reputation: 68

Getting PG:UndefinedTable: ERROR: when running migration to add friendships table (In development)

I'm trying to build an app that has users and friendships. I am using Rails 5, Ruby 2.4.0, and PostgreSQL 9.5.6

I'm currently trying to implement the add friend feature. I have added has_many associations for friendships, and for friends(through friendships, using the class of User).

I have found similar error messages related to not having the table being named created, however in my case the table friends, should just be my user table, aliased as friends.

When I try to run the migration to generate my friendships table I get the following error

-- 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)

Migration created when generating friendship model via:

rails g model friendship user:references friend:references

Here is the migration file

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

Here is my user model:

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

Do I need to add the association for friends, through friendships in the migration before I run it somehow?

Edit: Added migration file

Upvotes: 1

Views: 614

Answers (1)

disc0ninja
disc0ninja

Reputation: 68

Changing the migration to use index instead of foreign_key seems to have stopped the error message, and allow me the functionality I was after.

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

Upvotes: 2

Related Questions