GeezerVison is showing github.com — TML-3740: A MongoDB field with both @unique and @@index gets one unique index, and identical indexes in a collection merge instead of being refused by wmadden-electric · Pull Request #30894 · prisma/orm
TML-3740: A MongoDB field with both @unique and @@index gets one unique index, and identical indexes in a collection merge instead of being refused
#30894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
@@ -62,7 +62,7 @@ The builder checks the assembled contract last. Those refusals (kind `validator`
- One rule, one implementation. A rule added to the builder applies to every source.
- A source can report every refusal at once, at its own position, because refusals are data, not thrown errors.
- Sources cannot differ in what they accept. A parity suite writes each case in TypeScript and in PSL and checks that the two emitted `contract.json` files are byte-identical.
- Index order is the source's own. The builder does not reorder indexes, because the storage hash depends on their order.
- Index order is the source's own. The builder does not reorder indexes, because the storage hash depends on their order. When two indexes in one collection are identical, or one is a non-unique index made redundant by a unique index on the same keys and otherwise identical options, the builder removes one and keeps the survivor where it was written.
SQL does not follow this ADR fully yet. Its builder throws its first refusal, so SQL PSL places only the errors it catches and matches (TML-3730). SQL PSL also adds polymorphism to the contract after the builder runs, instead of carrying it in the definition (TML-2228).
@@ -256,7 +256,7 @@ The builder decides these behaviours the same way for every source:
| Behaviour | Every source |
| --- | --- |
| Two identical indexes in one collection, after variant scoping | Refused. The Prisma 6 reader first merges identical indexes, such as `@unique` and `@@unique` on one field, because the database holds one index for both |
| Identical or redundant indexes in one collection, after variant scoping | Merged into one. Two indexes are identical when they have the same keys and options, counting an option set to `false` as unset (`indexLookupKey`, which the planner also uses). A non-unique index is redundant beside a unique one that is otherwise identical. The unique one survives, else the first, and it keeps the position where it was written. `@unique` and `@@index` on one field give one unique index. The merge runs before the second text index is refused |
| `extensions` | The stack's extension packs |
| Roots | Explicit roots when the source gives them, else one per model with a collection, no owner and no base |
| A variant's collection | Its base's; another collection is refused |
@@ -535,7 +535,7 @@ The contract module at the configured path loads but exposes neither a `default`
### CONTRACT.NAME_DUPLICATE
Two declarations claim the same name: duplicate namespace entries, model names, value objects, relations, tables (two models mapping to one table, two table nodes for one table, or duplicate table in a namespace), column mappings (two fields to one column, a field and a table node's column node for one column, or two column nodes for one column), stored names (two fields of one MongoDB model stored under one name with `.mongo({ field })`: `Model "<Model>" stores both "<a>" and "<b>" as "<name>".`, `kind: 'field'`), indexes, value-sets (enum and pack entity minting the same value-set), or pack entities of the same kind and name in one namespace. A Postgres extension is unique across the whole contract: one declared in two namespaces is refused while building a contract and again when a contract is read (`kind: 'extension'`, `namespaces`). Payload: `kind`, `name`, `namespaceId`, `tableName` (for a column), `first`, `second`. A MongoDB variant field stored under a name a field of its base is stored under, the discriminator included, is refused too (`reason: 'stored-name-shared-with-base'`, with `first` the base's field and `second` the variant's): a variant's documents hold its base's fields, so the two would overwrite each other. Mongo PSL reports it as `PSL_DUPLICATE_STORED_NAME`.
Two declarations claim the same name: duplicate namespace entries, model names, value objects, relations, tables (two models mapping to one table, two table nodes for one table, or duplicate table in a namespace), column mappings (two fields to one column, a field and a table node's column node for one column, or two column nodes for one column), stored names (two fields of one MongoDB model stored under one name with `.mongo({ field })`: `Model "<Model>" stores both "<a>" and "<b>" as "<name>".`, `kind: 'field'`), value-sets (enum and pack entity minting the same value-set), or pack entities of the same kind and name in one namespace. A Postgres extension is unique across the whole contract: one declared in two namespaces is refused while building a contract and again when a contract is read (`kind: 'extension'`, `namespaces`). Payload: `kind`, `name`, `namespaceId`, `tableName` (for a column), `first`, `second`. A MongoDB variant field stored under a name a field of its base is stored under, the discriminator included, is refused too (`reason: 'stored-name-shared-with-base'`, with `first` the base's field and `second` the variant's): a variant's documents hold its base's fields, so the two would overwrite each other. Mongo PSL reports it as `PSL_DUPLICATE_STORED_NAME`.
@@ -7,4 +7,5 @@ Contract types and validation for the MongoDB family.
- **Contract types**: `MongoContract`, `MongoContractWithTypeMaps`, `MongoTypeMaps`, `MongoStorageShape` (raw-JSON storage shape) — the typed contract representation for MongoDB targets
- **Storage IR**: `MongoStorage` — the in-memory storage class instantiated by the per-target contract serializer; structurally satisfies `MongoStorageShape` and additionally carries the `namespaces` map
- **Type-level extraction**: `ExtractMongoTypeMaps`, `ExtractMongoCodecTypes`, `InferModelRow` — utility types for deriving codec types and row shapes from a contract
- **Index identity**: `MONGO_INDEX_OPTION_KEYS` lists every index option; the compiler rejects the list when an option of `MongoIndexInput` is missing. `indexLookupKey()` turns an index into a string from its keys and those options, with an option set to `false` counting as unset; `canonicalize()` writes option values with sorted keys. Two indexes are the same when their lookup keys are equal. The contract builder merges indexes, and the planner and the verifier match indexes, by this one function.
- **Structural schema + storage validation primitives**: `MongoContractSchema` (arktype envelope) and `validateMongoStorage()` — consumed by the family `ContractSerializerBase` to validate Mongo contracts at the deserializer seam. No freestanding `validateMongoContract` wrapper is exported; callers cross the seam via `familyInstance.deserializeContract(...)`.
readonly [Option in MongoIndexOptionKey]?: MongoIndexInput[Option] | undefined;
};
/** Identifies an index by its keys and the options it sets; an option set to `false` counts as unset. No field name or option value can make two different indexes share a key. */
export function indexLookupKey(index: MongoIndexLookupInput): string {
const options = Object.fromEntries(
MONGO_INDEX_OPTION_KEYS.flatMap((option) => {
const value = index[option];
return value === undefined || value === null || value === false ? [] : [[option, value]];
for (const fieldName of Object.keys(build.fields)) {
const symbol = build.located.symbol.fields[fieldName];
Expand DownExpand Up
@@ -1490,12 +1487,9 @@ function readRelationFields(
}
}
/**
* A model's indexes as the builder takes them, with where each is written. An index that its collection already holds, such as `@unique` and `@@unique` on one field, is left out: the database holds one index for both.
*/
/** A model's indexes as the builder takes them, with where each is written. */
it('reports a builder refusal where the schema writes its subject', async () => {
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.