0

Right now I have a User model and a Blog model.

class User < ApplicationRecord
  has_many :blogs, dependent: :destroy
end

class Blog < ApplicationRecord
  belongs_to :user
end

=> Blog(id: integer, user_id: integer, content: text, created_at: datetime, updated_at: datetime, title: string)

=> User(id: integer, name: string, email: string, username: string, number: integer, password_digest: string, created_at: datetime, updated_at: datetime, admin: boolean)

I used to have a Player model (pretty much the same as User model except for email/password) but got rid of it. But when I try to create a new Blog on heroku console it's giving me this error:

ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "blogs" violates foreign key constraint "fk_rails_eb906800d1"DETAIL:  Key (user_id)=(13) is not present in table "players".

What does this mean?

heroku logs

ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  insert or update on table "blogs" violates foreign key constraint "fk_rails_eb906800d1"

DETAIL:  Key (user_id)=(13) is not present in table "players".


class CreateBlogs < ActiveRecord::Migration[5.0]
  def change
    create_table :blogs do |t|
      t.references :user, foreign_key: true
      t.text :content

      t.timestamps
    end
  end
end

class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :username
      t.integer :number
      t.string :password_digest

      t.timestamps
    end
  end
end


class AddAdminToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :admin, :boolean, default: false
  end
end

class AddTitleToBlogs < ActiveRecord::Migration[5.0]
  def up
    add_column :blogs, :title, :string
  end

  def down
    remove_column :blogs, :title, :string
  end
end
iswg
  • 195
  • 1
  • 10
  • Did the Blog class belong to Player class in the past? How did you delete it? – Manali Nov 10 '16 at 00:37
  • @Manali yes it did. I deleted the Player model, and then got rid of the player_id table in Blog model by migration. – iswg Nov 10 '16 at 00:38
  • Could you post your migration code for Blog class (with the up and down methods)? – Manali Nov 10 '16 at 00:54
  • @Manali done. I think I deleted my remove player_id table migration file after running it on heroku – iswg Nov 10 '16 at 00:59
  • So the main foreign key violation you are getting is because there is still some trace in your app of the relation between Blog and Player. When you try to save a Blog with User as foreign key, it looks for that User in the Players table (which does not exist). You probably deleted the model in the wrong way. http://stackoverflow.com/questions/15648268/what-is-the-best-way-to-drop-a-table-remove-a-model-in-rails-3 Read this for the right way. I am not sure how I can help you correct this. Sorry. – Manali Nov 10 '16 at 01:28
  • @Manali that page and this page http://stackoverflow.com/questions/16928668/rails-create-a-drop-table-cascade-migration helped me make it work! Thank you for your help Manali – iswg Nov 10 '16 at 02:06

0 Answers0