feat(demos): add polymorphic-cms demo with single-table inheritance#232
Conversation
Agent-Logs-Url: https://github.com/scylladb/coodie/sessions/3e11763c-e528-4b00-af58-7d7f406d5aa5 Co-authored-by: fruch <340979+fruch@users.noreply.github.com>
Agent-Logs-Url: https://github.com/scylladb/coodie/sessions/2b2877a4-37cc-4453-9dc6-3387cfe1510f Co-authored-by: fruch <340979+fruch@users.noreply.github.com>
Content.sync_table() only creates columns defined on the base Content class. Subclass-specific columns (body, word_count, video_url, etc.) are never added to the Cassandra table, causing 'Unknown identifier' errors at runtime when saving subclass instances. Call sync_table() on each subclass after the base class so the driver issues ALTER TABLE ADD for the missing columns.
fruch
left a comment
There was a problem hiding this comment.
Review — Polymorphic CMS Demo
Summary
Well-structured demo that effectively showcases coodie's single-table inheritance via Discriminator. The code follows established demo conventions (file structure, Makefile targets, seed script patterns, FastAPI lifespan) and the HTMX UI is nicely themed.
Bug Fix (pushed)
Content.sync_table() does not create subclass-specific columns.
build_schema(Content) uses get_type_hints(Content) which only returns Content's own fields. Subclass columns like body, word_count, video_url, duration_seconds, resolution, audio_url, and episode_number are never added to the Cassandra table. When saving any subclass instance, the INSERT includes these columns and Cassandra returns Unknown identifier body.
Fix: call sync_table() on each subclass after the base class — the driver's sync_table issues ALTER TABLE ADD for columns present in the model but missing from the table. Pushed as commit 1d205e9.
Note: This arguably warrants a framework-level enhancement — sync_table() on a polymorphic base could automatically discover and sync all registered subclasses. Worth tracking as a separate issue.
Minor Observations
-
seed.pyline 234-236 — The fallbackdisc = type(item).__name__.lower()should never trigger since coodie auto-injects the discriminator value onsave(). Not harmful, but the defensive check is unnecessary. -
Demo README — The
How It Workssection shows the model definitions clearly. Good onboarding for users learning aboutDiscriminator.
Verdict
✅ Good to merge after CI passes on the fix commit. The demo is well-crafted and the only blocking issue (the sync_table bug) has been fixed.
…rent cache _schema() used hasattr() which traverses MRO, causing subclasses to return the parent's cached schema instead of building their own. This meant subclass-specific columns (e.g. body, word_count) were never included in sync_table() ALTER TABLE ADD statements. Aligns with the same __dict__ guard already used in build_schema().
demos/polymorphic-cms/seed.py