<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Brian Mills on Medium]]></title>
        <description><![CDATA[Stories by Brian Mills on Medium]]></description>
        <link>https://medium.com/@brianmills2718?source=rss-ab57631864d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*acB7xXbNtWhgmxCJ.jpg</url>
            <title>Stories by Brian Mills on Medium</title>
            <link>https://medium.com/@brianmills2718?source=rss-ab57631864d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 15:12:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@brianmills2718/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hypergraphs as an intermediate representation for schema translation]]></title>
            <link>https://medium.com/@brianmills2718/hypergraphs-as-an-intermediate-representation-for-schema-translation-2bfec08670e8?source=rss-ab57631864d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bfec08670e8</guid>
            <category><![CDATA[knowledge-graph]]></category>
            <category><![CDATA[hypergraph]]></category>
            <category><![CDATA[entity-relationship-model]]></category>
            <category><![CDATA[rdf]]></category>
            <category><![CDATA[schema-design]]></category>
            <dc:creator><![CDATA[Brian Mills]]></dc:creator>
            <pubDate>Fri, 04 Sep 2026 21:25:11 GMT</pubDate>
            <atom:updated>2026-09-04T21:25:11.124Z</atom:updated>
            <content:encoded><![CDATA[<p>A hypergraph is a graph whose edges may join any number of vertices, not only two. An intermediate representation is the form a compiler translates through. Used as one, a hypergraph keeps relationship structure in a single form across targets that each encode it differently.</p><p>A small compiler takes that literally. It reads one file describing a model as a hypergraph and writes PostgreSQL DDL, a MongoDB collection script, and a GraphQL schema. A binary relation is the special case where an edge has two endpoints. A relation over three or more is declared the same way and compiled the same way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gniZusgh64UUhso72bsHIQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*4xSlhO1NzuKTKrKfQO5XZA.gif" /></figure><p>It also reports which of the model’s declared constraints hold, and separates a constraint that passed from one it could not check.</p><p>Moving the structure turns out to be the easy half. PostgreSQL holds a three-way relation comfortably. What no target schema keeps is the record of <em>why</em> the structure is there: whether three foreign keys are one three-way fact or three unrelated references that happen to sit in a row. That is what the representation is for, and a round trip is how you measure it.</p><h3>The input</h3><p>A model declares vertex sets and hyperedges (relations that may connect more than two vertex sets). One file carries the whole example.</p><pre>system SocialNetwork {<br>    vertices users:  User  labeled String<br>    vertices groups: Group labeled String<br>    vertices posts:  Post  labeled String</pre><pre>    hyperedges friendships:        connect User User<br>    hyperedges memberships:        connect User Group<br>    hyperedges authorship:         connect User Post<br>    hyperedges group_interactions: connect User User Group<br>}</pre><pre>constraints {<br>    uniform(2)<br>    directed<br>    connected<br>}</pre><p>Three of the four hyperedges connect two vertex sets. The fourth connects three: two users and a group. That is an interaction between two people inside a specific group. It is a single fact about three participants, not three separate pairs.</p><h3>Why the fourth line is the interesting one</h3><p>A relational schema can state a three-way relation directly. One table, three foreign keys, one row per fact. The catalog exposes those foreign keys and their ordinal positions, so a tool can count them. What SQL does not record is why the three columns are there. They may be the participants of one ternary relation. They may be three independent references belonging to an entity. That is a modelling decision, and the schema keeps no place for it.</p><p>Binary-edge models cannot state it as a single edge. RDF triples and property graph edges join exactly two things. A three-way fact therefore needs an invented node: a blank node, or an employment vertex, carrying one edge per participant. That node is introduced to represent the relation as an object. Sometimes it deserves to be one: an employment contract has its own start date and salary. Sometimes it is only bookkeeping. The schema does not record which.</p><p>The cost is not the table. Declaring connect User User Group keeps the arity a statement the model makes rather than a shape a later tool has to infer, and the generator derives the junction table from that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*KBf09SL13XlHp1SDVw59og.png" /></figure><h3>The output</h3><p>group_interactions produces this PostgreSQL table.</p><pre>-- hyperedge group_interactions: arity 3, connects User + User + Group<br>CREATE TABLE group_interactions (<br>    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),<br>    user_1_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,<br>    user_2_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,<br>    group_id  UUID NOT NULL REFERENCES groups(id) ON DELETE CASCADE,<br>    UNIQUE (user_1_id, user_2_id, group_id)<br>);<br>CREATE INDEX group_interactions_user_1_id_idx ON group_interactions (user_1_id);<br>CREATE INDEX group_interactions_user_2_id_idx ON group_interactions (user_2_id);<br>CREATE INDEX group_interactions_group_id_idx  ON group_interactions (group_id);</pre><p>Repeated endpoints are numbered, not merged. Two User endpoints become user_1_id and user_2_id. The unique constraint spans all three columns, so the same pair interacting in two different groups is two rows.</p><p>The same declaration produces a MongoDB collection with a $jsonSchema validator and a compound unique index across the three references. In GraphQL it produces one type with three typed fields, reachable from both User and Group.</p><p>One line of model, three representations, no hand-written junction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*Fh-QkeFVqh5HiS2TQs5RFg.png" /></figure><p>The generated text is deterministic. No timestamps, no generated identifiers, no ordering that varies between runs. Two runs of the same model produce byte-identical files, so the output diffs cleanly in version control.</p><h3>What the representation is for</h3><p>The hypergraph is the form to translate <em>through</em>. Nothing is computed in it, so the only operation it has to support is emitting a schema. That is the one task where uniformity pays and operational fit does not matter.</p><p>Operational fit is what a general representation costs. A hypergraph encoding of a vector discards vector arithmetic, of a tree the hierarchical traversal, of a relation the join. Each paradigm exists because it is good at its own operations, and this schema-translation task asks for none of them. Translation in general can care about operational semantics; emitting a schema does not.</p><p>Arity 3 is where this earns its place. It is the smallest case where the three targets need materially different encodings, and the translation still succeeds into all of them.</p><p>A precise name for the object is worth having, because “hypergraph” is doing loose work above. connect User User Group names User twice, and a hyperedge in the textbook definition is a <em>set</em> of vertices, which cannot hold a repeat. The edges here join vertex <em>sets</em> rather than individual vertices, they are ordered, and their positions can carry roles. That makes this a typed, role-bearing n-ary incidence structure.</p><p>Which is close enough to Chen’s entity-relationship model that the overlap is worth stating rather than leaving for a reader to notice. ER has had n-ary relationship sets as a first-class construct since 1976, and mapping one to a junction table is textbook. Declaring the arity and deriving the table is that mapping. It is not a new idea and this piece should not imply otherwise.</p><p>What is not textbook is the rest of it: a four-state verdict that separates a constraint that held from one nothing could check, and a measurement of what a round trip through each target actually keeps.</p><h3>The constraint verdict</h3><p>The model declares three constraints. The tool reports on each, and on the constraints it supports that this model did not declare.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*d_BPTTZKH1j5YrfjzEX1Lg.png" /></figure><p>uniform(2) requires every hyperedge to connect exactly two vertex sets. The model declares it, and group_interactions connects three. The verdict names the constraint, the count, and the specific hyperedge that violates it.</p><p>connected holds. Two vertex sets are joined when some hyperedge names both, and the model is connected when every set is reachable that way. All three lie in one component.</p><p>directed is reported as <strong>unevaluated</strong>. The language expresses which vertex types a hyperedge connects. It does not express which way the relation runs. No structural test decides direction, so the tool reports the constraint as unchecked and states why.</p><p>That is a gap in the language and not only in the checker, and the article would be flattering itself to report it as a virtue alone. authorship runs from a user to a post. The junction table this tool emits does not record that, and no verdict state puts it back. A source language that cannot express direction generates schemas that lose it.</p><p>acyclic is reported as <strong>not applicable</strong>. The model does not declare it, so nothing is claimed about it.</p><h3>Why four states and not two</h3><p>Most validators report pass or fail. That merges two different situations: a constraint that was checked and held, and a constraint that could not be checked at all.</p><p>The merge is a reporting bug with a specific cost. A model whose constraints are half unverifiable reports the same green as a model whose constraints are all verified. The difference is exactly the information you needed.</p><p>Separating them makes the tool say something falsifiable about its own coverage. Three declared constraints, one holding, one violated, one unevaluated. You can act on that. “Two of three passed” hides which two.</p><p>This model makes that concrete. It declares directed, and a boolean checker has two options, both wrong. Reporting failure says the model breaks a rule it does not break. Reporting success says direction was verified when nothing examined it. The four-state verdict returns neither. It names the constraint and says no structural predicate exists to decide it, which is the only true answer available.</p><h3>What survives the round trip</h3><p>A reader recovers a model from the PostgreSQL this tool generated. It is bounded to exactly that. Reading back self-generated output is a much smaller problem than reading a database in general. It is enough to measure what the representation keeps.</p><p>Reading only executable SQL, it recovers the vertex sets and their fields, with nullability. It also recovers the hyperedge names, the endpoints in order, the participant roles and the symmetric pairs. Roles survive because they are column names. Symmetry survives because it is a CHECK constraint. Neither is a convention held somewhere outside the schema.</p><p>Three things do not survive. The vertex type names appear in the generated file only inside comments, so a reader restricted to executable SQL sees a table called users and cannot know its declared type was User. The system&#39;s own name is a comment too. And the declared constraints are evaluated when the schema is generated and never emitted at all. The reader reports each as lost rather than inventing it.</p><p>Admitting the comments recovers the names, and re-emitting reproduces the file byte for byte.</p><p>That is the measurement the argument above needs. What the intermediate representation keeps and the schema does not is not the structure. PostgreSQL holds the structure comfortably. It is the record of why the structure is there.</p><p>The same reader exists for MongoDB, and the two agree. Rendering both recovered models back to the source language produces identical text. Two front ends that share no parser recovered the same model from two different serialisations of it.</p><p>MongoDB keeps more in its executable form than PostgreSQL does. An endpoint’s target collection is a string inside the validator rather than a comment. A reader that ignores comments still finds it. The type names are lost in both, for the same reason.</p><h3>No target keeps everything, and they lose different things</h3><p>GraphQL has no reader yet, and it is the interesting one. SDL keeps the vertex type names in executable form, which is exactly what the other two lose to comments. It loses the symmetry they keep. And it carries a vertex set’s own name only through a camel-cased query field, so book_clubs and bookClubs arrive indistinguishable.</p><p>Line the three up and no single target preserves everything, but more usefully, they do not lose the same things. PostgreSQL loses the type names and keeps the symmetry. MongoDB keeps the endpoint targets executable and loses the type names too. GraphQL keeps the type names and loses the symmetry. A round trip through any one of them is lossy in a way a round trip through the representation is not, which is the case for having the representation at all.</p><h3>What it does not do</h3><p>The scope is small and stated, so the absences are deliberate rather than pending.</p><p>Operation bodies are parsed and ignored. No complexity analysis, no resolver generation. The verdict claims nothing about operations.</p><p>No instance data is loaded, validated, or migrated. Constraints are evaluated against the declaration only. Anything that is a property of rows rather than of the model is reported unevaluated.</p><p>No database is contacted. No server, no container, no connection string, no credential. Generated schemas are text, and the tool never executes them.</p><p>Translation runs both ways for two of the three targets. PostgreSQL and MongoDB have readers; GraphQL does not yet. Each reader reads only what this tool wrote, so this is a source format that targets three schema languages. Reading arbitrary PostgreSQL or MongoDB is a different problem, and a harder one.</p><p>Symmetry is enforced in two of the three targets. A participant can carry a role. connect a:User b:User symmetric(a, b) declares the two positions interchangeable, so (Alice, Bob) and (Bob, Alice) are one fact. PostgreSQL gets CHECK (a_id &lt;= b_id) beside the unique constraint, which refuses a reversed pair by name rather than reordering it quietly. MongoDB combines $jsonSchema with $expr in the collection validator. $jsonSchema describes one document at a time and cannot compare two of its own fields. GraphQL SDL has no built-in cross-field constraint, and a custom directive would need some server to implement it. Its schema carries a note naming the pair as declared and not enforced there. That is the same posture as an unevaluated constraint: say what is not being checked.</p><p>Vertex sets and relationships both carry fields. A block of email: String, age: Int, bio: String? becomes typed columns, validator properties and SDL fields. The ? carries nullability into all three. A relationship takes the same block, so an employment edge can hold a salary and a start date. What a field cannot yet carry is a constraint of its own, such as a salary that must be positive. It also makes a hyperedge a statement that the participants are related, not a record of an occurrence. group_interactions says this pair interacts in this group. It cannot say they did so eleven times, because a second row would violate the generated unique constraint.</p><p>A field naming a type the language does not know is refused rather than defaulted. An earlier version guessed columns from type names, so a set called users acquired name, email and created_at that no model declared. Guessed columns are worse than absent ones, because they look declared.</p><h3>Where it is useful</h3><p>The tool is worth reaching for under three conditions together. Your model has relations over more than two things. You are targeting more than one schema language. You want the arity and the constraints to stay declared rather than becoming a property of hand-written DDL.</p><p>Under those conditions it removes the hand-maintained junction tables and tells you which of your declared constraints are actually being enforced.</p><p>Outside them, an ORM or a migration tool covers the same ground with more features.</p><p>The honest summary of what this is: a prototype that makes one argument carefully, not a tool to point at a production schema. The argument is that what a schema loses in translation is usually not the structure. It is the record of why the structure is there.</p><p>The code is on [GitHub](https://github.com/BrianMills2718/hypergraph-schema-ir) if you want to run it against a model of your own.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bfec08670e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>