Hey friends! Does anyone have an example of handli...
# squarelibraries
a
Hey friends! Does anyone have an example of handling polymorphic models with SQLDelight? I found this example from around 5 years ago -- is it still the recommended approach?
a
I would say we intentionally don’t do any fancy OOP with the generated code to keep the library and its usage simple, I do use polymorphic types sometimes but I always create them in kotlin by selecting the data I need and then using the query function which takes a mapper
👍 1
So as soon as you want complex types, better to define them in kotlin and map to them manually
a
Makes sense -- I was (eventually) planning on mapping to my own types anyway Do you usually do 1. one big table with the union of all subtype fields + some discriminator, 2. a unique, self-contained table per subtype 3. a table with the parent class + separate tables for each subtype with ids as foreign keys to the parent-class table 4. something else I can't think of Does one of those approaches work better with Kotlin-side mapping than another?
a
We usually have something like 3 or 4 tables that get joined, and the columns that are nullable indicate what type to make it (the phone number is null so this is an email recipient, that kind of thing), however joins can get hairy. My general rule of thumb is to have the tables be easy to insert into since queries are easier to scale: so if you have all your data coming from one endpoint, it goes into one table, that kind of thing. I don’t like having multiple sources of truth for a table (like two separate classes modifying the same table). Hopefully that helps - essentially keep the writes super simple and let the queries be more complex
1
a
Makes sense! Thanks 🙂