I have 3 entities(Dishes, Places, Artifacts) which inherit from one abstract class called Memory. Every Memory can store up to 3 photos(string paths) so I set one to many relation Memory-Photo. Photo row contains memory Id as a foreign key so they can be linked together.
The problem is that Android Room creates separate tables for every type of memory so the @AutoGenerate = true annotation generates ids in 3 separate orders for every type of memory.
It causes conflicts because in database there might be 3 memories of different types with the same id so new set of photos would be linked to more than one memory.
I came up with an idea to make primary key out of creation timestamp but it is not the best idea. Maybe there is some way to synchronise key generation mechanism or change the plan of my database.
abstract class Memory(@ColumnInfo(name = "favorite") var favorite: Boolean,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "date") var date: Date,
@ColumnInfo(name = "description") var description: String,
@Embedded var mapMarker: MapMarker) : Serializable {
@ColumnInfo(name="id")
@PrimaryKey(autoGenerate = true)
var id: Long = 0 }
@Entity(tableName = "artifact_memories")
class ArtifactMemory(favorite: Boolean,
title: String,
date: Date,
description: String,
mapMarker: MapMarker) : Memory(favorite, title,
date, description, mapMarker){}
@Entity(tableName = "dish_memories")
class DishMemory(@ColumnInfo(name = "country_of_origin") var originCountry: String,
@ColumnInfo(name = "type") var dishType: String,
favorite: Boolean,
title: String,
date: Date,
description: String,
mapMarker: MapMarker) : Memory(favorite, title,
date, description, mapMarker) {}
@Entity(tableName = "photos")
data class Photo(@ColumnInfo(name="photo_path") var photoPath: String,
@ColumnInfo(name="memory_id") var memoryId: Long = 0,
@ColumnInfo(name="is_main") var main: Boolean = false) : Serializable {
@PrimaryKey(autoGenerate = true) var id: Long = 0 }
How to deal with such relationships? The goal is to make photos be saved within only one memory and/or remove conflicts with id duplicates.