Representing No Relation
While much of this book will use UUID
values for primary keys, plenty of other Room examples will use int
, particularly with autoGenerate
set to true
, to have SQLite generate the keys.
However, this does not work well if those keys will be used as foreign key values, in cases where there may be no value for the relation.
For example, Category
uses String
for its id
(created from a UUID
), and we represented a root category by means of having null
for its parentId
value. That works because String
fields can be null
.
If, however, we used int
, we have no way of representing the no-relation scenario. You cannot assign null
to an int
field in Java.
Hence, if you want to support the no-relation scenario, your foreign key field needs to allow for null
values. If you want to use auto-generated SQLite identifiers, use Integer
, not int
.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.