Repository navigation
Description
What happens
The SQL contract builder groups tables, models, enums and value sets by namespace in plain objects, and reads a group with ?? {}. A name that every JavaScript object inherits is read from the prototype, so the builder writes to a global object and the contract comes out wrong.
packages/2-sql-family/2-authoring/contract-ts/src/build-contract.ts:
const tablesByNamespace: Record<string, Record<string, StorageTableInput>> = {};
for (const table of tables) {
const namespaceTables = tablesByNamespace[table.namespaceId] ?? {};
namespaceTables[table.tableName] = lowerTable(table, tableLoweringContext);
tablesByNamespace[table.namespaceId] = namespaceTables;
}With a table users in a namespace of each name:
| Namespace name | Result |
|---|---|
__proto__ |
The lookup returns Object.prototype. The table is written onto it, so every object in the process gains a users property, and the table is missing from the contract. |
constructor |
The lookup returns the global Object function, and the table is written onto it. |
toString |
The lookup returns the built-in toString function, and the table is written onto it. |
The same lines, runnable on their own:
const tablesByNamespace = {};
const namespaceTables = tablesByNamespace['__proto__'] ?? {};
namespaceTables['users'] = { columns: {} };
tablesByNamespace['__proto__'] = namespaceTables;
Object.keys(tablesByNamespace); // []
({}).users; // { columns: {} }A target cannot guard against this. The hook a target has for column types runs once per column, so a model with no columns never reaches it, and the grouping above runs either way.
Reproduced on 8.0.0-rc.23 and on main.
Two defects
__proto__ cannot be stored as a key of a plain object. Assigning it sets the object's prototype and stores nothing. This holds one level down too: a table or model named __proto__ vanishes from its group. No fix inside the builder makes the name safe in later stages, which copy, merge and rebuild these objects.
Every other inherited name (constructor, toString, valueOf, hasOwnProperty and the rest) is a normal key once stored. The defect is the read before anything is stored: it returns the inherited value, so the ?? {} fallback never runs.
Proposed fix
- Refuse
__proto__wherever a name becomes a key in the SQL builder: namespace, table, column, model, field, enum, value set. The Mongo family already refuses a field or relation stored as__proto__(packages/2-mongo-family/1-foundation/mongo-contract/src/stored-names.ts), and the Postgres contract printer refuses it for the names it writes. - Read every grouping keyed by such a name with
Object.hasOwn, so an inherited value is never taken for a stored one. This covers the groupings by namespace inbuild-contract.ts(tables, models, enums, value sets) and the objects keyed by table and model name inside them.
constructor, toString and the like stay allowed. They are plausible table and column names in a real database, and they are reliable keys once the read is correct.
Tests
constructorandtoStringas a namespace name and as a table name: the table is in the emitted contract, the contract loads back fromcontract.json, and no global object changed.__proto__as a namespace, table, column, model, field and enum name: refused, and no global object changed.
Not in this issue
The same ?? {} read on a name-keyed object appears elsewhere in the SQL family, the Mongo family and the framework. Those need their own sweep.
Activity
wmadden-electric commentedon Nov 20, 2026
The Prisma team is working on this fix. Please do not open a pull request for it.
TML-3903: a namespace or table named constructor or toString builds a…