{"success":true,"name":"name-the-cardinality-before-creating-the-collection-one-to-o","description":"Adding a new entity that relates to existing ones (members, coaches, courts)","category":"ai-brimind","path":"ai-brimind/name-the-cardinality-before-creating-the-collection-one-to-o/SKILL.md","file":"SKILL.md","content":"---\nname: name-the-cardinality-before-creating-the-collection-one-to-o\ndescription: \"Adding a new entity that relates to existing ones (members, coaches, courts)\"\n---\n\n# Name the Cardinality Before Creating the Collection: One-to-One, One-to-Many, or Many-to-Many Changes Everything\n\n## When to use\nYou are about to create a collection/model that connects to existing entities, and nobody has stated whether the relationship is 1-1, 1-many, or many-many — now and in a year.\n\n## Example request\n'A member belongs to a club' modeled as member.clubId → then the product grows 'members can join several clubs' and every query, screen, and index built on the single-club assumption must be rebuilt; the cardinality question was skipped, not answered. 'A coach teaches many groups, a group has many coaches' → that is many-to-many, and it wants its OWN linking collection (coachId + groupId + role/date fields); arrays of IDs stuffed on one side work until the relation itself needs data. The relation carries facts of its own — since when, what role, what price → that is the signal the LINK is an entity: membership is not an arrow between member and club, it is a record with a life (and your state-machine discipline applies to it). A settings object per user → true 1-1, embed or same-document per your embed/reference discipline; cardinality decides WHICH discipline even applies.\n\n## Instructions\nAct as a data modeler who refuses to create any collection before saying its relationships out loud in plain sentences — 'a member belongs to exactly one club, forever?' 'a booking connects one member to one court at one time?' — because cardinality (one-to-one, one-to-many, many-to-many) is the single decision everything downstream inherits: where the foreign key lives, whether a linking collection exists, which queries are cheap, which indexes matter, and what your delete-design must handle. Getting it wrong is not a bug, it is a foundation poured in the wrong shape. For every new relationship, do this: (1) State the cardinality in both directions, twice: as the product IS today, and as it plausibly grows ('could a member ever belong to two clubs? could a session ever have two coaches?'). Ask the owner if unsure — this is a business question wearing a schema costume, and guessing it is guessing the product. The upgrade from 1-many to many-many is among the most expensive migrations in app development; five minutes of asking beats it. (2) Let the answer place the data mechanically: 1-1 → embed or same document (your embed/reference discipline takes over); 1-many → the ID lives on the MANY side (member.clubId, never an unbounded members array on the club — your unbounded-array rule), indexed per your index discipline because every 'members of this club' query filters on it; many-many → a linking collection with both IDs, full stop — arrays of IDs on either side re-create the unbounded-growth trap and cannot hold facts about the link. (3) Check whether the link itself is secretly an entity: the moment the relationship carries its own data — joinedAt, role, price, status — the linking record is a first-class model with a name (Membership, Enrollment, Booking), its own lifecycle (your state-machine discipline), and its own screens; many of the most important collections in club-style apps ARE reified relationships, and recognizing that early is the difference between a clean domain and a pile of patch fields. (4) Wire the consequences immediately, not later: compound unique index on the linking pair if duplicates are illegal (member cannot join the same club twice — your normalize-and-unique discipline), the parent-ID indexes for both directions of lookup, and the delete plan for each side per your orphans discipline. (5) Record the decision in one line where the model is defined ('1 club — N members; membership may become M-N per roadmap, see #') so the next developer inherits the reasoning, not just the shape. Only create the collection once each of its relationships has a stated cardinality, a placed key, and a delete plan — a schema whose relationships were never said out loud is not designed, it is transcribed from the first example that came to mind.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #243\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}