SwiftData: class inheritance without entity inheritance

In SwiftData, how can I have class inheritance without entity inheritance? My understanding is when models are subclassed they then share the same SQLite table allowing one Query to return multiple model types. However what if we don't require that functionality and only wish to share some overridable logic.

I.e. share a base class implementation with multiple @Model subclasses but each model is in it's own SQLite table.

E.g. say I want a savedAt timestamp field in every model and have the logic for setting it across all models. Or do some validation that calls super to check parent implementation is valid too.

Maybe the validation behaviours can be done using protocols and extensions some how?

Thanks!

The recommended approach is to utilize protocol inheritance to get the desired behavior you described. The protocol's can implement default implementations that conformers could override when desired.

It is also covered in "SwiftData: Dive into inheritance and schema migration"

SwiftData: class inheritance without entity inheritance
 
 
Q