django orm Q

sihwan_e·2022년 2월 22일
0

목적 :
소셜채널 테이블에 조회하려는 인플루언서 아이디로 인스타그램 채널이 등록되어있는가?
해결은 django orm의 Q를 통해 복잡한 질의를 해결했다.

if not SocialChannel.objects.filter(
            Q(influencer=influencer.id) & Q(social_channel=SocialChannelType.INSTAGRAM.value)
        ).exists():

처음엔 급한대로 아래처럼 했지만

if not SocialChannel.objects.filter(
            influencer=influencer.id, social_channel=SocialChannelType.INSTAGRAM.value
        ).exists():

아래 것을 조회 했을 때,
django.db.utils.IntegrityError: duplicate key value violates unique constraint "member_socialchannels_pkey"
DETAIL: Key (id)=(7868) already exists.

profile
Sometimes you gotta run before you can walk.

0개의 댓글