Pg:Error column does not exist

trosborn

I am trying to make it so a user has_many goals, and then only let a user see their own goals. My welcome controller where the code is failing:

class WelcomeController < ApplicationController
  def index
    if user_signed_in?
      @goals = current_user.goals.all
    else
      @goals = Hash.new
    end
  end
end

User.rb is like so:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :goals
end

And my Goal.rb:

class Goal < ActiveRecord::Base
  belongs_to :user
end

The error I am getting is:

PG::Error: ERROR: column goals.user_id does not exist LINE 1: SELECT "goals".* FROM "goals" WHERE "goals"."user_id" = $1 ^ : SELECT "goals".* FROM "goals" WHERE "goals"."user_id" = $1

(I get this error while logged in, after having created a new goal)

I tried adding "foreign_key :user_id" to my User model but then I get the following error:

/Users/Thomas/newyears/app/models/user.rb:7: syntax error, unexpected tSYMBEG, expecting keyword_do or '{' or '(' has_many :goals, foreign_key :user_id ^

In my routes file on line 3, which looks like this:

Goalsy::Application.routes.draw do
  get "mycalendar/index"
  devise_for :users
  devise_for :goals
  resources :goals

  get "welcome/index"
  resources :posts

  root 'welcome#index'
end
Bachan Smruty

I guess, the user_id column is missing in the goals table.

Just run the command in your console to create the migration

rails generate migration AddUserRefToGoals user:references

It will generate the migration file.

class AddUserRefToGoals < ActiveRecord::Migration
  def change
    add_reference :goals, :user, index: true
  end
end

Then do the rake db:migrate

Hope, it will fix your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ActionView::Template::Error (PG::UndefinedColumn: ERROR: column "weeknumber" does not exist

From Dev

PG::UndefinedColumn: ERROR: column comments.post_id does not exist

From Dev

PG::UndefinedColumn: ERROR: column photos.attachable_type does not exist

From Dev

queue classic returns PG::UndefinedColumn: ERROR: column "pid" does not exist

From Dev

PG::UndefinedColumn: ERROR: column sections.row_order does not exist

From Dev

Rake db:migrate error 'PG::UndefinedColumn: ERROR: column "slug" does not exist'

From Dev

Rails: PG::UndefinedTable: ERROR: relation "..." does not exist

From Dev

PG::UndefinedTable: ERROR: relation does not exist

From Dev

PG::UndefinedTable: ERROR: relation "musicians" does not exist

From Dev

Sidekiq PG::UndefinedColumn: ERROR: does not exist

From Dev

PG::UndefinedTable: ERROR: relation 'caves' does not exist

From Dev

Custom foreign_key in model gives PG::Error column does not exist - Rails

From Dev

PG::UndefinedColumn: ERROR: column "city_id" of relation "events" does not exist

From Dev

pg_search using associated_against gives error "column [model_name].[associated_column_name] does not exist"

From Dev

pg_search using associated_against gives error "column [model_name].[associated_column_name] does not exist"

From Dev

Receiving the error PG::UndefinedColumn: ERROR: column mymodels.distance does not exist when using Geocoder's near method

From Dev

Receiving the error PG::UndefinedColumn: ERROR: column mymodels.distance does not exist when using Geocoder's near method

From Dev

PG::UndefinedObject: ERROR: type "hstore" does not exist but it does

From Dev

No such column error when column does exist

From Dev

No such column error when column does exist

From Dev

rake aborted! PG::UndefinedTable: ERROR: relation "pages" does not exist

From Dev

Getting PG::UndefinedFunction: ERROR: operator does not exist: integer ~~* integer

From Dev

PG::UndefinedTable: ERROR: table "table-name" does not exist

From Dev

pg_restore error: role XXX does not exist

From Dev

rake aborted! PG::UndefinedTable: ERROR: relation "pages" does not exist

From Dev

PG::UndefinedFunction: ERROR: operator does not exist: character varying @> point

From Dev

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "guestbooks" does not exist

From Dev

pg_restore ERROR: “Relation does not exist” and creating new database

From Dev

Heroku + Apartment PG::Error: ERROR: function pg_stat_statements_reset() does not exist

Related Related

  1. 1

    ActionView::Template::Error (PG::UndefinedColumn: ERROR: column "weeknumber" does not exist

  2. 2

    PG::UndefinedColumn: ERROR: column comments.post_id does not exist

  3. 3

    PG::UndefinedColumn: ERROR: column photos.attachable_type does not exist

  4. 4

    queue classic returns PG::UndefinedColumn: ERROR: column "pid" does not exist

  5. 5

    PG::UndefinedColumn: ERROR: column sections.row_order does not exist

  6. 6

    Rake db:migrate error 'PG::UndefinedColumn: ERROR: column "slug" does not exist'

  7. 7

    Rails: PG::UndefinedTable: ERROR: relation "..." does not exist

  8. 8

    PG::UndefinedTable: ERROR: relation does not exist

  9. 9

    PG::UndefinedTable: ERROR: relation "musicians" does not exist

  10. 10

    Sidekiq PG::UndefinedColumn: ERROR: does not exist

  11. 11

    PG::UndefinedTable: ERROR: relation 'caves' does not exist

  12. 12

    Custom foreign_key in model gives PG::Error column does not exist - Rails

  13. 13

    PG::UndefinedColumn: ERROR: column "city_id" of relation "events" does not exist

  14. 14

    pg_search using associated_against gives error "column [model_name].[associated_column_name] does not exist"

  15. 15

    pg_search using associated_against gives error "column [model_name].[associated_column_name] does not exist"

  16. 16

    Receiving the error PG::UndefinedColumn: ERROR: column mymodels.distance does not exist when using Geocoder's near method

  17. 17

    Receiving the error PG::UndefinedColumn: ERROR: column mymodels.distance does not exist when using Geocoder's near method

  18. 18

    PG::UndefinedObject: ERROR: type "hstore" does not exist but it does

  19. 19

    No such column error when column does exist

  20. 20

    No such column error when column does exist

  21. 21

    rake aborted! PG::UndefinedTable: ERROR: relation "pages" does not exist

  22. 22

    Getting PG::UndefinedFunction: ERROR: operator does not exist: integer ~~* integer

  23. 23

    PG::UndefinedTable: ERROR: table "table-name" does not exist

  24. 24

    pg_restore error: role XXX does not exist

  25. 25

    rake aborted! PG::UndefinedTable: ERROR: relation "pages" does not exist

  26. 26

    PG::UndefinedFunction: ERROR: operator does not exist: character varying @> point

  27. 27

    ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "guestbooks" does not exist

  28. 28

    pg_restore ERROR: “Relation does not exist” and creating new database

  29. 29

    Heroku + Apartment PG::Error: ERROR: function pg_stat_statements_reset() does not exist

HotTag

Archive