Search code examples
androidandroid-room

How to define and use a tag system for my TODO app in room database


I've got a simple TODO app, each TODO is stored in the room datase. But I want to add tags for these TODO's so that each TODO could have 0 or more tags and each tag is a siple string. So the question is: what is the best solution for this situation? How to store this set of tags for each TODO?


Solution

  • You want a many-many relationship. This entails using a third table in addition to the TAG table and the TODO table. Such a table has two columns one that references the TAG table, the other that references the TODO table.

    The PRIMARY KEY, should be a composite PRIMARY KEY made up of both columns (so a TODO/TAG combination must be unique i.e. you don't want a TODO to have the same tag twice).

    This answer covers this in SQLite terms What is the best way to design a tag-based data table with Sqlite?

    This answer covers this from a Room perspective Relationship many to many between objects not working for me in Android Room