Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

다대다 구조 Relation을 이용해서 구현 / Fix: Foreign Key Constraint Fail… #53

Merged
merged 3 commits into from
Nov 23, 2022

Conversation

Choe-Ji-Hwan
Copy link
Member

…ed 에러 해결 (#52 (comment))

🚀 Issue #5 + #52

  • save되는 것의 @Insert(onConflict = OnConflictStrategy.REPLACE) 로 인해서, SQLite Foreign Key Constraint Failed (code 787) 에러 발생
    -> 해결: @Insert(onConflict = OnConflictStrategy.IGNORE) 이후, 중복 값이 들어온다면 체크 후, id를 바꿔서 보내주는 형태

👨‍🔧 개요

  • saveMoments 내용 안정화

📝 작업 내용

  • MomentDao에 GlobeId, PictureId를 가져오는 쿼리문 함수 추가
  • MomentDataSource에 중복 내용에 대한 작업 처리 (Picture 부분)
  • 다대다 구조 Relation로 형태 변경
  • saveMoment할 때, globe는 하나를 선택하므로, 하나만 가져오는 로직으로 변경

📢 특이 사항

  • 각 Entity의 id는 이름이 ~_entity_id 이런식으로 변경되었는데 Relation 변경 작업에서 이것저것 하다보니 생겼습니다. 네이밍 리팩터링 필요해 보여요! 참고 바랍니다

@Choe-Ji-Hwan Choe-Ji-Hwan requested review from BBongKim and jhg3410 and removed request for BBongKim November 23, 2022 09:13
@Choe-Ji-Hwan Choe-Ji-Hwan self-assigned this Nov 23, 2022
@Choe-Ji-Hwan Choe-Ji-Hwan added this to the 2주차 스프린트 milestone Nov 23, 2022
Copy link
Member

@BBongKim BBongKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다 👏

@@ -37,15 +52,20 @@ class MomentRepositoryImpl @Inject constructor(
return
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분에 else가 필요 없어 보입니다. 😃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞네요 early return

OR detailAddress LIKE '%' || :query || '%'
OR content LIKE '%' || :query || '%'
ORDER BY
CASE WHEN :sortType = 0 THEN date END DESC,
CASE WHEN :sortType = 1 THEN date END ASC
"""
)
fun getMoments(sortType: Int = 0, query: String): PagingSource<Int, MomentEntity>
fun getMoments(sortType: Int = 0, query: String): PagingSource<Int, MomentWithGlobesAndPictures>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

검색 기능이 이미 구현된건가요? 제 담당이긴 한데 😅
query 없이 모먼트를 가져오려면, query = ""를 파라미터 값으로 넘겨줘야 할까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

검색 기능이 재민님이 구현하신 내용인데 검색 없이는 "" 맞습니다!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 검색 쿼리는 원래 있었는데 저는 정렬만 추가했습니다!


@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMoment(moment: MomentEntity): Long

@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun savePicture(picture: List<PictureEntity>): List<Long>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헷갈리지 않도록 s를 추가해서 savePictures()로 변경하는게 어떨까요?

Copy link
Member Author

@Choe-Ji-Hwan Choe-Ji-Hwan Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 좋습니다!


@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMoment(moment: MomentEntity): Long

@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun savePicture(picture: List<PictureEntity>): List<Long>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMomentPicture(momentPictures: List<MomentPictureEntity>)
Copy link
Member

@BBongKim BBongKim Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 같은 맥락으로 s 를 추가해서 saveMomentPictures() 어떨까요?

뭔가 Picture에만 s가 붙는게 이상하게 느껴지신다면,

MomentPictureCrossRefEntity 또는 MomentPictureXRefEntity 와 같이 Entity이름을 변경하신다면

saveMomentPictureCrossRefs()로 표기할 수 도 있을 것 같습니다.

공식 문서에도 사용하는 네이밍 방식입니다.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최대한 폴더 이름과 같게 하려고 했어요 entity 폴더니까 entitiy로 끝내야된다..? 따라서 그런데 공식 문서처럼 바꾸는 것이 좋은 네이밍 같아요. 뒤에만 s붙이는건 오해의 여지가 있으니까..? 변경하겠습니다.
MomentGlobeEntity -> MomentGlobeXRef
MomentPictureEntity -> MomentPictureXRef

함수 네이밍

suspend fun saveMomentPictures(momentPictures: List<MomentPictureXRef>)
->
suspend fun saveMomentPictureXRefs(momentPictures: List<MomentPictureXRef>)
suspend fun saveMomentGlobe(momentGlobe: MomentGlobeXRef)
->
suspend fun saveMomentGlobeXRef(momentGlobe: MomentGlobeXRef)

Copy link
Member

@jhg3410 jhg3410 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다 🙂


@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun saveMoment(moment: MomentEntity): Long

@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.IGNORE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IGNORE 가 존재했군요!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 들어가는 걸 update하는 것이 아니라 ignore하는 겁니다! 들어간다면, 업데이트된 id를 다시 잇는 과정보다는 찾는 과정이 더 빠를 것 같아서 선택했습니다

@@ -10,6 +10,6 @@ import androidx.room.PrimaryKey
indices = [Index(value = ["name"], unique = true)]
)
data class GlobeEntity(
@PrimaryKey(autoGenerate = true) val id: Long = 0L,
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "globe_entity_id") val id: Long = 0L,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모든 entity 의 id 를 동일하게 한다면, 중간에 entity 를 빼고 동일하게 해도 좋을 것 같습니다!🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 아까 이야기한 것처럼 id 이름에 entity 모두 제거했습니다!

Comment on lines 52 to 59
val indexResult = momentDao.savePicture(pictures).toMutableList()
indexResult.forEachIndexed { pictureIndex, id ->
if (id == EXIST_INSERT_ERROR_CODE) {
indexResult[pictureIndex] = momentDao.getPictureByByteArray(pictures[pictureIndex].bitmap)
}
}
return indexResult.toList()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저장 한 Picture 들의 아이디를 들고 와서 충돌이 났다면 해당 인덱스는 업데이트된 아이디를 넣어주는 듯 하군요!! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 충돌 난 것만 다시 가져오고 리스트로 반환하는 방식으로 로직을 변경했습니다. ignore 당한 id-1로 받아서요!

@Choe-Ji-Hwan Choe-Ji-Hwan merged commit 82ef039 into develop Nov 23, 2022
@Choe-Ji-Hwan Choe-Ji-Hwan deleted the feature/data/refactor-getmoments branch November 23, 2022 12:42
@Choe-Ji-Hwan Choe-Ji-Hwan changed the title refactor: 다대다 구조 Relation을 이용해서 구현 / Fix: Foreign Key Constraint Fail… 다대다 구조 Relation을 이용해서 구현 / Fix: Foreign Key Constraint Fail… Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants