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