Skip to content

fix(typegen): disambiguate embeds of opposite-direction keys and junction tables, alias embeds named like columns - #2020

Merged
spydon merged 2 commits into
mainfrom
lukasklingsbo/sdk-2359-fixtypegen-generated-embeds-are-ambiguous-for-opposite
Oct 23, 2026
Merged

spydon merged 2 commits into
mainfrom
lukasklingsbo/sdk-2359-fixtypegen-generated-embeds-are-ambiguous-for-opposite

Conversation

@spydon

@spydon spydon commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes SDK-2359.

Problem

Some foreign key shapes produced embeds that PostgREST could not serve:

  1. Keys in opposite directions. With profile.team -> team and team.owner_id -> profile, the generator only added a !fkey hint when two keys pointed the same way. Profile.teamByTeam and Profile.teamViaOwnerId both sent select=team(*), which PostgREST rejects with PGRST201.
  2. Embed keys named like a column. With badge.profile -> profile, selectOnly([Badge.profile, Badge.profileByProfile.select()]) sent select=profile,profile(*). The response then carries a duplicate profile key, so reading the column fails.
  3. A key next to a junction table (found in review, and the case the ticket names as the most common). With badge.team -> team and a team_badge junction whose primary key covers its keys to both tables, PostgREST sees a many-to-one and a many-to-many relationship, so the unhinted team(*) is ambiguous too.

All three were reproduced against the local stack before the fix.

Fix

  • The generator counts the relationships PostgREST sees between each pair of tables once per run. That covers foreign keys in either direction, plus many-to-many relationships through a junction table whose primary key covers a key to each table. A unique constraint is not enough, which was verified on the stack. A relation carries the hint whenever its pair has more than one relationship.
  • An embed is aliased to its member name in snake case when it is hinted (as before) or when its plain key is also the name of a column or computed field of the table. Aliases are still kept apart from every other key of the table.

Generated member names do not change. Embeds of the affected shapes now resolve and come back under their own keys.

Tests

  • The hostile fixture gains team, profile, badge and the team_badge junction, covering all three shapes, and the hostile golden is regenerated. New generator tests assert the hints and the alias, and that a junction whose primary key does not cover both keys adds no relationship.
  • A new migration, supabase/migrations/20240101000005_embed_disambiguation.sql, creates the same tables, and supabase/seed.sql fills them. generated_embeds_integration_test.dart embeds them through the members in the hostile golden.
  • Because of that test, supabase_typegen now runs with the local stack in CI (backend, concurrency 1), and AGENTS.md lists it among the stack-backed packages. The list now also includes supabase, which it was missing.

The integration test is deliberately left untagged, like the other stack-backed suites in the repository, so the package runs the same way every other backend package does.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 4 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between 6083d66 and 22d1223.

📒 Files selected for processing: 10
  • .github/workflows/test.yml
  • AGENTS.md
  • packages/supabase_typegen/lib/src/dart_generator.dart
  • packages/supabase_typegen/pubspec.yaml
  • packages/supabase_typegen/test/dart_generator_test.dart
  • packages/supabase_typegen/test/generated_embeds_integration_test.dart
  • packages/supabase_typegen/test/goldens/hostile_fixture.dart
  • packages/supabase_typegen/test/goldens/hostile_schema.dart
  • supabase/migrations/20240101000005_embed_disambiguation.sql
  • supabase/seed.sql
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

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

@spydon spydon changed the title fix(typegen): disambiguate embeds of opposite-direction keys and alias embeds named like columns fix(typegen): disambiguate embeds of opposite-direction keys and junction tables, alias embeds named like columns Oct 22, 2026
@spydon
spydon merged commit 587a253 into main Oct 23, 2026
45 of 46 checks passed
@spydon
spydon deleted the lukasklingsbo/sdk-2359-fixtypegen-generated-embeds-are-ambiguous-for-opposite branch October 23, 2026 12:53
spydon added a commit that referenced this pull request Oct 23, 2026
…anges met (#2046)

Three combinations of PRs that were each green on their own broke main.

- `supabase_auth`: #2027 added `pkce_failed_request_test.dart` passing
`HttpMethod.x.value`, but #2025 changed `requestsTo` and the stub
helpers to take an `HttpMethod`, so the package no longer analyzes.
- `supabase_test`: since #2027, a local `signOut()` revokes the session
in the background. The `stubSignOut` test looked for the `/logout`
request right after `signOut()` returned, so it now waits for the event
queue first.
- `supabase_typegen`: #2020 added a `badge` table to the hostile
fixture, and #2044 suffixes generated names that clash with Flutter
types, so the class is now `BadgeTable`. The hostile golden is
regenerated, and the tests that reference the class use the new name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant