I am using Minitest and Postgres to test my Rails website. When I added a “Like” model, I started getting Postgresql deadlock errors
Minitest::UnexpectedError: ActiveRecord::Deadlocked: PG::TRDeadlockDetected: ERROR: deadlock detected LINE 7: DELETE FROM "videos"; ^ DETAIL: Process 11456 waits for RowExclusiveLock on relation 42608 of database 42434; blocked by process 10460. Process 10460 waits for ShareRowExclusiveLock on relation 42564 of database 42434; blocked by process 11456. HINT: See server log for query details.
Other errors:
Minitest::UnexpectedError: ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "accounts_pkey"
DETAIL: Key (id)=(825136781) already exists.
Minitest::UnexpectedError: ActiveRecord::ConnectionNotEstablished: connection is closed
My models:
class Like < ApplicationRecord
belongs_to :profile
belongs_to :video
end
class Video < ApplicationRecord
belongs_to :profile
has_one_attached :source
has_and_belongs_to_many :likes_profiles, class_name: 'Profile', join_table: 'likes'
#Some validations...
end
class Profile < ApplicationRecord
belongs_to :account
has_many :followers_follow, foreign_key: :followee_id, class_name: 'Follow', dependent: :destroy
has_many :followers, through: :followers_follow, source: :follower, dependent: :destroy
has_many :followees_follow, foreign_key: :follower_id, class_name: 'Follow', dependent: :destroy
has_many :followees, through: :followees_follow, source: :followee, dependent: :destroy
has_many :videos, dependent: :destroy
has_and_belongs_to_many :liked_videos, class_name: 'Video', join_table: 'likes'
has_one_attached :avatar
# Some validations...
end
Like model tests:
require "test_helper"
class LikeTest < ActiveSupport::TestCase
def setup
@profile1 = profiles(:basic1)
@profile2 = profiles(:basic2)
@video1 = videos(:one)
end
test 'profile should be able to like video' do
f = Like.new(profile_id: @profile2.id, video_id: @video1.id)
assert f.save
assert @profile2.liked_videos.include?(@video1)
assert @video1.likes_profiles.include?(@profile2)
end
test 'should not like second time' do
f = Like.new(profile_id: @profile1.id, video_id: @video1.id)
assert_not f.save
end
end
Everything was good until I added Like model