MichaelScaria
MichaelScaria

Reputation: 1590

How to update reference column in Ruby

I have a migration that I want to make with a reference in my table. I create the reference using this:

create_table :user_events do |t|
  t.references :user, :null => false
end

And in my migration, I want to be able to allow the reference to be NULL.

def self.up
  change_column :user_events, :user, :integer, :null => true
end

However I keep getting PGError: ERROR: column "user" of relation "user_events" does not exist. Am I migrating wrong?

Upvotes: 0

Views: 1152

Answers (3)

sameera207
sameera207

Reputation: 16619

try

def self.up
  change_column :user_events do |c|
    c.references :user, :integer, :null => true  
  end 
end

Upvotes: 0

Kaeros
Kaeros

Reputation: 1138

It's because your migration creates a column named user_id, referencing the User model.

Upvotes: 1

doesterr
doesterr

Reputation: 3965

This should work:

def self.up
  change_column :user_events, :user_id, :integer, :null => true
end

Note that the column you're trying to change is called user_id, not user

Upvotes: 3

Related Questions