Skip to content

Protocol format needs support for inheritance #1618

Closed

Description

@lukehutch

Is your feature request related to a problem? Please describe.

I need a way to create a database table whose columns are a superset of the columns of another database table. In other words, I need to be able to extend a protocol file, and have the generated subclass extend the generated superclass.

I have several needs for this, but principally, for law enforcement purposes, I need to preserve records for something like 3 months after a user tries to delete their account, with these requirements:

  • I need to be able to move user data from the primary table for active users over to an archive table.
  • I need the archive table to have at least the same columns as the primary table.
  • However, I also want to be able to add some more columns to the archive table, indicating the date that the record was archived, and the action that caused the record to be archived.
  • I don't want to just store the JSON serialization of rows of the primary table in a single column of the backup table, because I need the archived data to be queryable too.

Furthermore, I need to be able to store copies of certain record types, for statistical purposes. For example, I manually implemented the boilerplate needed to get templated queries to work (#1392), and now my Profile protocol class can serve as a database row, or a set of query criteria to find matching profiles. I want to be able to store the user's latest Profile query template in a separate table from the main profile table, so that I can run statistics over that table to see which profile options users most commonly search for.

Describe the solution you'd like

Basically this would look like something like

class: Profile
table: profile
fields:
  userId: int
  name: String
  age: int
class: ProfileArchive
extends: Profile
table: profile_archive
fields:
  archivedDate: DateTime
  archivedReason: String

and then ProfileArchive would have the following fields (and its table profile_archive would have the same database columns): userId, name, age, archivedDate, and archivedReason.

Describe alternatives you've considered

I don't currently see a way to set up the database declaratively to allow a protocol file to be stored in two tables (much less where one table has additional columns).

Activity

  1. Isakdl commented on Dec 1, 2023

    @Isakdl
    Collaborator

    This sounds like you need view tables, issue tracked here: #228 related discussion on syntax here: #932

  2. added
    enhancement
    on Dec 1, 2023
  3. lukehutch commented on Dec 1, 2023

    @lukehutch
    ContributorAuthor

    I don't think so: CREATE VIEW creates a view, not a table, and views are not materialized. I need an actual physical copy of the row in a second table, as an archive or backup, because I want to be able to delete the original after a copy of it is archived.

  4. lukehutch commented on Dec 1, 2023

    @lukehutch
    ContributorAuthor

    At the most basic level, I want to be able to write protocol objects of a given class to multiple different tables, not as serialized JSON, but as column values. Right now, tables in Serverpod can only be written to one table. The only way to allow a protocol class to write to more than one table (given the current MyProtocolClass.db syntax) is through inheritance. Fortunately, that allows for the adding of additional columns, which I also need for adding metadata to the archived copies.

  5. Isakdl commented on Dec 4, 2023

    @Isakdl
    Collaborator

    Got it! Yeah in that case a view table is not gonna cut it. For your usecase you would not need some automatic sync between the tables? The archive is a deliberate action?

    Or is the archive here more like an event log, of previous states?

  6. lukehutch commented on Dec 4, 2023

    @lukehutch
    ContributorAuthor

    Correct, no automatic sync is needed.

    For my use case, I need to be able to simply take a snapshot of records, and also add extra metadata, such as the timestamp of the snapshot.

    In the more general case, this feature will be generally useful for several reasons:

    • Records of the same type (or at least in a column subset/superset relationship) will be able to be stored in more than one table (currently one protocol object type can only be stored in one table). This could for example help when there are low latency demands, by pre-joining data and storing it in a cache table, to avoid the cost of a join.
    • It will be possible to have a proper inheritance hierarchy between protocol classes, which presents a lot of interesting opportunities, and can probably solve a lot of different types of problems. Maybe you want a superclass HasUserId that includes a userId column, and every class that contains a userId can inherit from that class (this may even be useful to Serverpod, for automatically offering joins on supertype columns);

    ...and there were some other usecases I thought of that I can't remember right now :-)

  7. lukehutch commented on Dec 18, 2023

    @lukehutch
    ContributorAuthor

    Here's another usecase that I need this for:

    I wrote my own code for templated queries (#1392). Basically I have a large protocol type called Profile with many columns, and I want to be able to send a partially-filled in Profile object to the server, to return all profile rows that match any non-null values in the submitted query template. This all works.

    Now what I want to be able to do is to record the most recent profile query template for each user, as a rough sample of the types of queries users are running against the database (one sample per user, consisting of their most recent query template only). So I need a second table that I can store Profile objects in, so that I can do some statistics on the query templates. This needs to be stored separately from the main profile table, because these aren't profiles, they are query templates.

    I don't want to have to duplicate the profile.yaml definition for another table -- and anyway, I can't store objects from one Profile class in the table of another Profile class, even if the protocol definitions are identical.

    What I want to be able to do is list more than one table that a given protocol object can be stored in. The best way to do this is by inheritance, because then fields/columns can be added to the subclasses (which solves for other needs I described previously).

  8. lukehutch commented on Jan 1, 2024

    @lukehutch
    ContributorAuthor

    I have yet another usecase for inheritance within protocol classes.

    I have a very large Profile protocol object, with hundreds of fields. If a user fetches their own profile, then all fields should be returned. However, if a user is fetching a profile view of another user, certain private fields should not be returned. Currently, I null-out the non-public profile fields before returning them, if the user is not requesting their own profile -- in other words, I return a subset of the fields (which fits the superclass pattern).

    This would better be handled by inheritance, because I have a 3rd even smaller subset of fields that I return when displaying a list of profile thumbnails. I don't need the entire profile, just the user's name, thumbnail image URL, and a couple of other fields. This is a further subset (or would be a superclass of the public profile superclass).

    Allowing inheritance in this way would also speed up queries, because if you only need a subset of the columns, then only the requested subset of columns would need to be returned from the database, deserialized to Dart, then re-serialized for sending over the wire.

  9. lukehutch commented on Feb 2, 2024

    @lukehutch
    ContributorAuthor

    I would even settle for the ability to list multiple table names in a single model definition, all of which are mapped to a single class definition:

    class: Profile
    table: profile, profile_archive   # <-------
    fields:
      userId: int
      name: String
      age: int
  10. added
    area: models
    on Dec 18, 2024
  11. marcelomendoncasoares commented on Apr 3, 2026

    @marcelomendoncasoares
    Collaborator

    Closing as completed with the introduction of polymorphism on #4227.

  12. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Apr 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: modelsenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions