Repository navigation
TML-3789: PostgreSQL Json stores jsonb by default, and Json(storage: json) keeps a json column - #30966
TML-3789: PostgreSQL Json stores jsonb by default, and Json(storage: json) keeps a json column#30966wmadden-electric wants to merge 19 commits into
Conversation
…PlainJson, reviewed by an architect and a principal engineer Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…lt, and the json type is PlainJson Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…ite Json Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…age: json) for a json column Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…lainJson, and the planner asks before turning a json column into jsonb Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…nvenience type over Jsonb and PlainJson Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…rage: json), and the Prisma port fixtures store jsonb Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…keep json or move to jsonb Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…over and print as Jsonb and PlainJson Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… and moved to jsonb with migration plan --delete Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… first as the scalar convenience types ADR, and the index lists Json among them Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…umns infer writes, and leaves SQLite and MongoDB Json names to TML-3790; ADR 257 says why PlainJson is not named after its database type Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
… schema copies, and detect type.Json({}) and imports from @prisma/orm-target-postgres
Signed-off-by: willbot <[email protected]>
Signed-off-by: Will Madden <[email protected]>
…json) against a live database, and db verify reports the json to jsonb mismatch Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…as deferred Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…y --schema-only names the Json column Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…onb or json, and an as option Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
@prisma/orm-extension-arktype-json
@prisma/orm-extension-middleware-cache
@prisma/orm-extension-paradedb
@prisma/orm-extension-pgvector
@prisma/orm-extension-postgis
@prisma/orm-extension-supabase
@prisma/orm-family-mongo
@prisma/orm-family-sql
@prisma/orm-framework
@prisma/orm-mongo
@prisma/orm-mysql
@prisma/orm-postgres
@prisma/orm-postgres-contrib
@prisma/orm-sqlite
@prisma/orm-target-mongo
@prisma/orm-target-mysql
@prisma/orm-target-postgres
@prisma/orm-target-sqlite
@prisma/orm-toolchain
commit: |
size-limit report 📦
|
|
Closing without merging. PostgreSQL |
|
Correction to the closing note above: |
…son still gives json, and sql.String(n) is now sql.Varchar(n) (prisma#31065) ```prisma model Event { id Int @id payload Json // a json column, as before search Json(storage: jsonb) // a jsonb column code sql.Varchar(12) // was sql.String(12) } ``` ## What changes for a user **`Json` is a convenience type on PostgreSQL.** It has one option, `storage`: | Written | Explicit type | Column | | --- | --- | --- | | `Json` or `Json(storage: json)` | `PlainJson` | `json` | | `Json(storage: jsonb)` | `Jsonb` | `jsonb` | - No schema, contract or database changes. A field written `Json` keeps its `json` column. `pnpm fixtures:check` shows no difference. - `contract infer` and `contract print` write a `json` column as `Json` and a `jsonb` column as `Json(storage: jsonb)`. They wrote `Jsonb` before. `Jsonb` remains valid. - The Prisma 7 reader writes Prisma 7's `Json`, which was `jsonb`, as `Json(storage: jsonb)`, and `Json @db.Json` as `Json`. - In a TypeScript contract, `type.Json()` and `type.Json({ storage: 'jsonb' })` are typed as the explicit type they choose. The column helper `jsonColumn` is renamed `plainJsonColumn`, with no alias. - The reference docs say what a `json` column cannot do (equality filters, indexes, unique constraints) and that `Json(storage: jsonb)` can. A new integration test shows the index and unique limits against PostgreSQL. **`sql.String(n)` is renamed `sql.Varchar(n)`** on PostgreSQL, SQLite and MySQL. `sql.String` stays for one release line as a deprecated alias: in PSL it warns `PSL_DEPRECATED_SCALAR_NAME` and emits the same contract; in TypeScript it still compiles. Both renames have upgrade instructions with detection patterns in `upgrade-instructions/pending/postgres-json-convenience-type/`. ## Why - **`Json` defaults to `json`.** `Json` is the exact name of a PostgreSQL type, so it must not mean a different one. A user who wants `jsonb` says so. - **`PlainJson` exists because a convenience type and an explicit type cannot share a name.** A type choice names its explicit type in the same namespace. Users meet `PlainJson` in hover text, the type reference, and the refusal of `@db.Json`. The tools never write it. - **`sql.String` shared a name with the `String` convenience type and meant something else** (a `varchar(n)` column). The namespace is now `sql.Varchar(n)` and `sql.Char(n)`. ## ADRs - ADR 316 gains `Json`, the rule for naming an explicit type whose natural name is a convenience name, the reason `BigInt` is kept as its own concept, and the `sql.Varchar` rename. ADR 257 and ADR 279 are amended to match. - Main had two ADRs numbered 316. The one about an extension pack's extension and the search path is now ADR 321, in its own commit (`f1f5d2a83b`). ADR 320 is taken by prisma#30926. ## Also in this pull request `projects/scalar-convenience-types/spec.md` and `plan.md` hold the design for the rest of the project: the same eight convenience types on MongoDB, SQLite and MySQL, each in a later pull request. ## Verification - `/drive-code-review` (system design and code passes), two rounds; the second is clean on the pushed tip, with all acceptance criteria for this change passing. - Manual QA with the real CLI against a local PostgreSQL: emit, refusals, `db init`, `contract infer`, `contract print` round-trip, `db verify`, a TypeScript contract, and the Prisma 7 reader. - No source file under `packages/1-framework` changes. ## Known gaps, tracked separately - `Json @unique` or an index on a `Json` field fails only when the migration runs, with PostgreSQL's own message, which does not mention `Json(storage: jsonb)`. TML-3833 covers a clear error. - The TypeScript compiler suggests `jsonbColumn` for the removed `jsonColumn`. The upgrade instruction names the right replacement. ## Alternatives considered - **Default `Json` to `jsonb`.** Built in prisma#30966 and closed: it changed every existing `Json` column and made a PostgreSQL type name mean another type. - **No convenience type for `Json`.** Leaves no way to say "a JSON value" and choose its storage, which every other scalar now has. - **Name the explicit type `JsonJson`**, by the value-suffix rule. It says nothing. - **Rename `sql.String` inside the SQLite pull request.** PostgreSQL is the released target it affects, so it ships here. Closes TML-3789. Agent: genghis-36 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * PostgreSQL `Json` now supports `json` storage by default and `jsonb` via an explicit storage option. The explicit `json` type is named `PlainJson`. * Variable-length SQL text types are now named `sql.Varchar`; `sql.String` remains available as a deprecated alias. * The PostgreSQL untyped JSON column helper is now named `plainJsonColumn`. * **Documentation** * Updated reference materials and migration guidance to explain the new names, storage choices, and JSON limitations, including unsupported equality filtering, indexing, and unique constraints for `json`. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
On PostgreSQL,
Jsonbecomes a convenience type, likeIntandStringin #30942. It means "a JSON value". Bare, it storesjsonb.Json(storage: json)stores plainjson. Both read aJsonValue.JsonJsonbpg/jsonb@1jsonbJson(storage: json)PlainJsonpg/json@1jsonPlainJsonis the new name of the type thatJsonnamed until now. The naming rule would give itJson, which the convenience type now takes. No suffix for the JavaScript value separates it fromjsonb, because both read the same values, so it takes a qualifier: plainjson, as opposed tojsonb. ADR 316 records this as a naming rule.This changes existing contracts
A PostgreSQL field written
Jsonin an existing Prisma 8 schema moves from ajsoncolumn to ajsonbcolumn:prisma contract emitwrites a new storage hash. Nothing warns, becauseJsonis still the right name to write. A type choice cannot carry a deprecation warning without a framework change.prisma db verifyreports a hash mismatch, andprisma db verify --schema-onlynames the column:The contract column is "jsonb" and the database column is "json".prisma migration planandprisma db updatedo not change the column on their own. The planner treatsjsontojsonbas a change that can lose data, and asks about each field. Answer--delete Event.payloadto accept PostgreSQL's in-place conversion, or--convert Event.payloadonmigration planto write your own. It stays a data-loss question on purpose:jsonbdrops duplicate keys, normalizes whitespace and key order, and refuses the\u0000escape thatjsonaccepts.jsoncolumn, writeJson(storage: json)orPlainJson. The contract does not change.Not affected:
Jsonb.Jsonalready meantjsonb, andJson @db.Jsonreads asPlainJson.This is the second change to
Jsonin the release candidates. The 0.16-to-0.17 upgrade boundJsontojsonand told users who meantjsonbto writeJsonb. Those users are not affected now. ADR 316 records the change as an exception to ADR 257's rule that a rename keeps a deprecated alias, and says why.The app and extension upgrade instructions are in
upgrade-instructions/pending/postgres-json-convenience-type/.The tools
contract inferandcontract printwriteJsonfor ajsonbcolumn andJson(storage: json)for ajsoncolumn. A printed schema that showedJsonbnow showsJson.type.Json()is typed asJsonbandtype.Json({ storage: 'json' })asPlainJson.jsonColumnis renamedplainJsonColumn, after its explicit type.jsonbColumnkeeps its name.field.json()preset now says to writeJson.Repository schemas
Schemas that mirror a real
jsoncolumn keep it withJson(storage: json): the Supabase contract, the engines native-types port, and the collection-methods fixture. Theircontract.jsonfiles are unchanged. The Prisma port fixtures that translate upstream Prisma'sJson, which isjsonb, now storejsonb.The Supabase contract was edited by hand. Its generator already does not reproduce the committed contract on
main(TML-3598).Also in this PR
Two ADRs were numbered 316 on
main. This project's ADR merged first, so thesearch_pathADR from #30956 is renumbered 318, with its links.Alternatives considered
Jsondefaults tojson. That would avoid the contract change. It was rejected becausejsonbis what users should get by default: it can be indexed and compared, and Prisma 7, Django, Ecto, MikroORM, Hibernate and Gel all default to it.JsonText. Rejected because it reads as JSON kept in a text column, which is what SQLite'sJsonis.pg.Jsonin the target's namespace. Not possible: a type choice row names a type constructor at its own level of the namespace, and changing that changes the framework.Jsonfor one release line. Not possible without a framework change, and it would fire on the name users should write.Closes TML-3789.
Agent: dostoevsky-67
Summary by CodeRabbit
Jsonnow defaults tojsonb; useJson(storage: json)orPlainJsonto selectjson.jsonfromjsonb.jsonfield to bareJsonmay require a migration tojsonband explicit consent.plainJsonColumn.