Repository navigation
Description
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: intclass: ProfileArchive
extends: Profile
table: profile_archive
fields:
archivedDate: DateTime
archivedReason: Stringand 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).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
Isakdl commentedon Dec 1, 2023
This sounds like you need view tables, issue tracked here: #228 related discussion on syntax here: #932
lukehutch commentedon Dec 1, 2023
I don't think so:
CREATE VIEWcreates 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.lukehutch commentedon Dec 1, 2023
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.dbsyntax) is through inheritance. Fortunately, that allows for the adding of additional columns, which I also need for adding metadata to the archived copies.Isakdl commentedon Dec 4, 2023
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?
lukehutch commentedon Dec 4, 2023
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:
HasUserIdthat includes auserIdcolumn, and every class that contains auserIdcan 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 :-)
lukehutch commentedon Dec 18, 2023
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
Profilewith many columns, and I want to be able to send a partially-filled inProfileobject 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
Profileobjects 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.yamldefinition for another table -- and anyway, I can't store objects from oneProfileclass in the table of anotherProfileclass, 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).
lukehutch commentedon Jan 1, 2024
I have yet another usecase for inheritance within protocol classes.
I have a very large
Profileprotocol 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.
lukehutch commentedon Feb 2, 2024
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:
marcelomendoncasoares commentedon Apr 3, 2026
Closing as completed with the introduction of polymorphism on #4227.