Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

fix(sql-orm-client): don't type required to-one relations as nullable - #269

Merged
aqrln merged 1 commit into
mainfrom
tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1
Apr 1, 2026
Merged

aqrln merged 1 commit into
mainfrom
tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1

Conversation

@aqrln

@aqrln aqrln commented Mar 31, 2026 •

Copy link
Copy Markdown
Member

Fix the types of N:1 relationships to not mark them as nullable when the foreign key is not nullable.

The situation with 1:1 is a little weird since we only represent them once in the contract (unlike 1:N which are represented both ways) and we don't seem to populate the foreignKeys in the generated contract (which is a separate bug) so the fix still doesn't work for them. I'll look into that in a follow up PR.

Closes: https://linear.app/prisma-company/issue/TML-2120/includerelationvalue-type-incorrectly-marks-non-nullable-n1-relations

Summary by CodeRabbit

  • Bug Fixes

    • Type handling for to-one relations now marks included rows nullable only when the schema requires it, eliminating extra non-null assertions in tests.
  • New Features

    • Relation nullability is inferred from declared schema and foreign-key info, improving correctness for to-one vs to-many includes.
    • Expanded schema coverage to include profile, invited-by, and article reviewer scenarios.
  • Tests

    • Strengthened and added type-level and integration tests to validate various relationship nullability and shape behaviors.

@coderabbitai

coderabbitai Bot commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Type-level logic now computes to-one include nullability by inspecting relation parent columns and foreign keys; include types reflect that. The contract schema adds profiles and articles with explicit foreign keys. Tests and an example integration test were updated to align assertions with the refined include nullability.

Changes

Cohort / File(s) Summary
Type System Core
packages/3-extensions/sql-orm-client/src/types.ts
Imported StorageTable; added internal type utilities to extract relation parent columns, detect parent-column nullability and FK-side ownership, and compute IsToOneRelationNullable. Updated IncludeRelationValue to conditionally allow null for to-one includes based on that computation.
Type-level Tests
packages/3-extensions/sql-orm-client/test/include-cardinality.test-d.ts
Added Profile and Article include assertions, new exported row aliases (UsersWithInvitedByRow, ProfilesWithUserRow, ArticlesWithReviewerRow), and strengthened assertions verifying nullable vs non-nullable to-one shapes and exact key sets.
Schema Fixtures
packages/3-extensions/sql-orm-client/test/fixtures/contract.ts
Added explicit foreign keys (users.invited_by_id, posts.user_id, comments.post_id, profiles.user_id), declared profiles primary key, introduced articles table/model, and added profiles.user and articles.reviewer relations.
Integration Tests (examples)
examples/prisma-next-demo/test/repositories.integration.test.ts
Removed local intersection type casting and non-null assertion workarounds in assertions; directly assert mapped post ID arrays and access included user.id without !.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

🐇 I nibbled schema lines with care,
Followed keys that hid elsewhere.
Now includes know when nulls may stay,
Tests hopped in to show the way.
Tiny paws, precise and fair.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically identifies the main change: fixing TypeScript type definitions for non-nullable to-one relations in the sql-orm-client, which aligns directly with the primary objective described in the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Mar 31, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/runtime-executor

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/runtime-executor@269

@prisma-next/mongo-core

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-core@269

@prisma-next/mongo-orm

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-orm@269

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/mongo-runtime@269

@prisma-next/sql-runtime

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-runtime@269

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-paradedb@269

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/extension-pgvector@269

@prisma-next/postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/postgres@269

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-orm-client@269

@prisma-next/target-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-mongo@269

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-mongo@269

@prisma-next/driver-mongo

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-mongo@269

@prisma-next/contract-authoring

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract-authoring@269

@prisma-next/contract-ts

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract-ts@269

@prisma-next/ids

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/ids@269

@prisma-next/psl-parser

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/psl-parser@269

@prisma-next/psl-printer

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/psl-printer@269

@prisma-next/cli

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/cli@269

@prisma-next/emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/emitter@269

@prisma-next/eslint-plugin

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/eslint-plugin@269

@prisma-next/migration-tools

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/migration-tools@269

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/vite-plugin-contract-emit@269

@prisma-next/sql-contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract@269

@prisma-next/sql-errors

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-errors@269

@prisma-next/sql-operations

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-operations@269

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-schema-ir@269

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-psl@269

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-ts@269

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-contract-emitter@269

@prisma-next/family-sql

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/family-sql@269

@prisma-next/sql-kysely-lane

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-kysely-lane@269

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-lane-query-builder@269

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-relational-core@269

@prisma-next/sql-builder-new

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-builder-new@269

@prisma-next/sql-lane

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/sql-lane@269

@prisma-next/target-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/target-postgres@269

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/adapter-postgres@269

@prisma-next/driver-postgres

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/driver-postgres@269

@prisma-next/core-control-plane

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/core-control-plane@269

@prisma-next/core-execution-plane

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/core-execution-plane@269

@prisma-next/config

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/config@269

@prisma-next/contract

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/contract@269

@prisma-next/operations

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/operations@269

@prisma-next/plan

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/plan@269

@prisma-next/utils

npm i https://pkg.pr.new/prisma/prisma-next/@prisma-next/utils@269

commit: 112aabc

@aqrln
aqrln force-pushed the tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1 branch from 2befd06 to 4551264 Compare March 31, 2026 13:05
@aqrln
aqrln marked this pull request as ready for review March 31, 2026 13:13

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
examples/prisma-next-demo/test/repositories.integration.test.ts (1)

401-406: Stale TODO comment references the issue being fixed.

The TODO references TML-2120, which this PR addresses. However, this comment is about a different problem—included relations being omitted from the result type entirely—rather than the nullability issue fixed here. Consider updating or removing this comment if the issue is now closed, or updating the reference if this is tracked separately.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@examples/prisma-next-demo/test/repositories.integration.test.ts` around lines
401 - 406, The TODO comment referencing TML-2120 and IncludeRelationValue is
stale and misleading relative to the current fix; update or remove it near the
UserWithPosts type (and the surrounding comment block that mentions
IncludeRelationValue and users) so it accurately reflects the remaining issue or
is dropped if resolved—either replace the TODO with a correct issue reference
and a short note about why the manual type assertion for UserWithPosts remains,
or delete the comment entirely if TML-2120/IncludeRelationValue is now fixed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@examples/prisma-next-demo/test/repositories.integration.test.ts`:
- Around line 401-406: The TODO comment referencing TML-2120 and
IncludeRelationValue is stale and misleading relative to the current fix; update
or remove it near the UserWithPosts type (and the surrounding comment block that
mentions IncludeRelationValue and users) so it accurately reflects the remaining
issue or is dropped if resolved—either replace the TODO with a correct issue
reference and a short note about why the manual type assertion for UserWithPosts
remains, or delete the comment entirely if TML-2120/IncludeRelationValue is now
fixed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 89ec6b37-b4da-498b-9c6a-25253f87d34c

📥 Commits

Reviewing files that changed from the base of the PR and between b0409e1 and 4551264.

📒 Files selected for processing (3)
  • examples/prisma-next-demo/test/repositories.integration.test.ts
  • packages/3-extensions/sql-orm-client/src/types.ts
  • packages/3-extensions/sql-orm-client/test/include-cardinality.test-d.ts

@aqrln
aqrln force-pushed the tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1 branch from 4551264 to d6f4f28 Compare March 31, 2026 13:30
@SevInf

SevInf commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

Would it make sense to add a test that ensures that if we have a relation that is NOT backed by foreign key then relation type is still nullable, even if the key field is not? As far as I understood the types this is how they should work and this is a right thing

@aqrln

aqrln commented Mar 31, 2026

Copy link
Copy Markdown
Member Author

As far as I understood the types this is how they should work and this is a right thing

As the types are currently written, that's only the case for 1:1 (and it's only used to determine which side of the relation we are on, and not because of the referential integrity) but you are right, this is a right thing to do in general. I'll update this and add the tests.

@aqrln
aqrln marked this pull request as draft March 31, 2026 15:49
@aqrln
aqrln force-pushed the tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1 branch 2 times, most recently from 397aa10 to d7faa15 Compare March 31, 2026 17:07
@aqrln
aqrln marked this pull request as ready for review March 31, 2026 17:08
@aqrln
aqrln force-pushed the tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1 branch from d7faa15 to 7a66cae Compare March 31, 2026 17:09
Fix the types of N:1 relationships to not mark them as nullable when
the foreign key is not nullable.

The situation with 1:1 is a little weird since we only represent them
once in the contract (unlike 1:N which are represented both ways) and
we don't seem to populate the foreignKeys in the generated contract
(which is a separate bug) so the fix still doesn't work for them. I'll
look into that in a follow up PR.

Closes: https://linear.app/prisma-company/issue/TML-2120/includerelationvalue-type-incorrectly-marks-non-nullable-n1-relations
@aqrln
aqrln force-pushed the tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1 branch from 7a66cae to 112aabc Compare March 31, 2026 17:13

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/3-extensions/sql-orm-client/src/types.ts`:
- Around line 978-983: The type RelationParentCols is checking the wrong
property name; change its conditional to look for on.parentColumns (and
similarly update any checks for childCols to on.childColumns) so
RelationParentCols correctly infers the parent column tuple from Relation, and
ensure IsToOneRelationNullable and other downstream types now use that canonical
parentColumns/childColumns shape; update the type guard in RelationParentCols
and the child column check (referencing RelationParentCols and the code that
previously inspected childCols) to use parentColumns/childColumns respectively.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 83a0541b-2c00-4d96-80d1-a95ab54feb00

📥 Commits

Reviewing files that changed from the base of the PR and between 7a66cae and 112aabc.

⛔ Files ignored due to path filters (2)
  • packages/3-extensions/sql-orm-client/test/fixtures/generated/contract.d.ts is excluded by !**/generated/**
  • packages/3-extensions/sql-orm-client/test/fixtures/generated/contract.json is excluded by !**/generated/**
📒 Files selected for processing (4)
  • examples/prisma-next-demo/test/repositories.integration.test.ts
  • packages/3-extensions/sql-orm-client/src/types.ts
  • packages/3-extensions/sql-orm-client/test/fixtures/contract.ts
  • packages/3-extensions/sql-orm-client/test/include-cardinality.test-d.ts
✅ Files skipped from review due to trivial changes (1)
  • examples/prisma-next-demo/test/repositories.integration.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/3-extensions/sql-orm-client/test/fixtures/contract.ts

@aqrln
aqrln merged commit 298238d into main Apr 1, 2026
14 checks passed
@aqrln
aqrln deleted the tml-2120-includerelationvalue-type-incorrectly-marks-non-nullable-n1 branch April 1, 2026 08:41
wmadden added a commit that referenced this pull request Apr 1, 2026
Resolve conflicts from rebasing onto origin/main:
- Add FK constraints to storage tables in contract.d.ts/json fixtures
  (required by PR #269 to-one relation nullability logic)
- Add Article model with ADR 172 structure and Profile user relation
- Update RelationParentCols to RelationLocalFieldColumns: resolve
  localFields (field names) to column names via FieldColumnName for
  FK constraint matching in IsToOneRelationNullable
wmadden added a commit that referenced this pull request Apr 1, 2026
Resolve conflicts from rebasing onto origin/main:
- Add FK constraints to storage tables in contract.d.ts/json fixtures
  (required by PR #269 to-one relation nullability logic)
- Add Article model with ADR 172 structure and Profile user relation
- Update RelationParentCols to RelationLocalFieldColumns: resolve
  localFields (field names) to column names via FieldColumnName for
  FK constraint matching in IsToOneRelationNullable
wmadden added a commit that referenced this pull request Apr 1, 2026
Resolve conflicts from rebasing onto origin/main:
- Add FK constraints to storage tables in contract.d.ts/json fixtures
  (required by PR #269 to-one relation nullability logic)
- Add Article model with ADR 172 structure and Profile user relation
- Update RelationParentCols to RelationLocalFieldColumns: resolve
  localFields (field names) to column names via FieldColumnName for
  FK constraint matching in IsToOneRelationNullable
jkomyno pushed a commit that referenced this pull request Apr 1, 2026
…#269)

Fix the types of N:1 relationships to not mark them as nullable when the
foreign key is not nullable.

The situation with 1:1 is a little weird since we only represent them
once in the contract (unlike 1:N which are represented both ways) and we
don't seem to populate the foreignKeys in the generated contract (which
is a separate bug) so the fix still doesn't work for them. I'll look
into that in a follow up PR.

Closes:
https://linear.app/prisma-company/issue/TML-2120/includerelationvalue-type-incorrectly-marks-non-nullable-n1-relations

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Type handling for to-one relations now marks included rows nullable
only when the schema requires it, eliminating extra non-null assertions
in tests.

* **New Features**
* Relation nullability is inferred from declared schema and foreign-key
info, improving correctness for to-one vs to-many includes.
* Expanded schema coverage to include profile, invited-by, and article
reviewer scenarios.

* **Tests**
* Strengthened and added type-level and integration tests to validate
various relationship nullability and shape behaviors.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants