feat(sql-contract-psl): support composite primary keys#432
feat(sql-contract-psl): support composite primary keys#432
Conversation
Accept model-level @@id attributes in the SQL PSL interpreter, including mapped storage columns and mapped constraint names. Add diagnostics for duplicate field/model primary keys, nullable composite-key fields, and unknown key fields. Add the task brief for the WS5 composite primary key slice.
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughThis PR implements support for model-level composite primary keys ( ChangesModel-Level Composite Primary Keys Support
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
@prisma-next/mongo-runtime
@prisma-next/family-mongo
@prisma-next/sql-runtime
@prisma-next/family-sql
@prisma-next/extension-arktype-json
@prisma-next/middleware-telemetry
@prisma-next/mongo
@prisma-next/extension-paradedb
@prisma-next/extension-pgvector
@prisma-next/postgres
@prisma-next/sql-orm-client
@prisma-next/sqlite
@prisma-next/target-mongo
@prisma-next/adapter-mongo
@prisma-next/driver-mongo
@prisma-next/contract
@prisma-next/utils
@prisma-next/config
@prisma-next/errors
@prisma-next/framework-components
@prisma-next/operations
@prisma-next/ts-render
@prisma-next/contract-authoring
@prisma-next/ids
@prisma-next/psl-parser
@prisma-next/psl-printer
@prisma-next/cli
@prisma-next/emitter
@prisma-next/migration-tools
prisma-next
@prisma-next/vite-plugin-contract-emit
@prisma-next/mongo-codec
@prisma-next/mongo-contract
@prisma-next/mongo-value
@prisma-next/mongo-contract-psl
@prisma-next/mongo-contract-ts
@prisma-next/mongo-emitter
@prisma-next/mongo-schema-ir
@prisma-next/mongo-query-ast
@prisma-next/mongo-orm
@prisma-next/mongo-query-builder
@prisma-next/mongo-lowering
@prisma-next/mongo-wire
@prisma-next/sql-contract
@prisma-next/sql-errors
@prisma-next/sql-operations
@prisma-next/sql-schema-ir
@prisma-next/sql-contract-psl
@prisma-next/sql-contract-ts
@prisma-next/sql-contract-emitter
@prisma-next/sql-lane-query-builder
@prisma-next/sql-relational-core
@prisma-next/sql-builder
@prisma-next/target-postgres
@prisma-next/target-sqlite
@prisma-next/adapter-postgres
@prisma-next/adapter-sqlite
@prisma-next/driver-postgres
@prisma-next/driver-sqlite
commit: |
| const primaryKeyName = modelPrimaryKey?.name ?? fieldPrimaryKeyName; | ||
| const isVariantModel = model.attributes.some((attr) => attr.name === 'base'); | ||
| if (primaryKeyColumns.length === 0 && !isVariantModel) { | ||
| if (primaryKeyColumns.length === 0 && !isVariantModel && modelPrimaryKeyAttributes.length === 0) { |
There was a problem hiding this comment.
Super nitpicky nit: keep !isVariantModel at the end of the condition, the order reads a bit weird right now.
Or even extract const hasPrimaryKey = primaryKeyColumns.length > 0 || modelPrimaryKeyAttributes.length > 0 and use !hasPrimaryKey here.
Intent
Support greenfield SQL PSL schemas that model junction tables with composite primary keys.
contract infercan already print composite keys as@@id([...]); this makes the PSL interpreter accept that same shape and lower it into the existing SQL contract primary-key node.Change map
The story
@@idattributes as primary-key declarations instead of falling through to the unsupported model-attribute path.@mapand@@mapcontinue to determine storage names.Behavior changes & evidence
Model-level composite primary keys are accepted: PSL models can declare
@@id([orgId, userId], map: "membership_pkey")without field-level@id, and the emitted storage contract uses the mapped storage columns in order.Missing-primary-key diagnostics understand
@@id: models that provide@@idno longer trigger the SQL-provider missing primary key diagnostic.Invalid composite primary keys report targeted diagnostics: duplicate field/model primary keys, nullable composite-key fields, and unknown composite-key fields now produce
PSL_INVALID_ATTRIBUTE_ARGUMENTdiagnostics instead of generic unsupported-attribute behavior.Compatibility / migration / risk
This is additive for supported greenfield PSL. Existing field-level
@idbehavior remains unchanged, and schemas that combine field-level@idwith model-level@@idnow fail explicitly instead of being reported through unsupported model-attribute diagnostics.Follow-ups / open questions
@updatedAt, and inline@db.*remain separate WS5 M2 slices.interpreter.defaults.test.ts,composed-mutation-defaults.test.ts, andprovider.test.ts.Non-goals / intentionally out of scope
@updatedAtor inline@db.*support.Summary by CodeRabbit
New Features
@@id([fieldA, fieldB])syntax in SQL PSL contracts.mapparameter.Tests
Documentation