Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit cd55433

Browse files
committed
Revert "fix: errors with relationships duplicates"
This reverts commit ac6165c.
1 parent ac6165c commit cd55433

File tree

2 files changed

+6
-82
lines changed

2 files changed

+6
-82
lines changed

src/select-query-parser/utils.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@ type FilterRelationNodes<Nodes extends Node[]> = UnionToArray<
3737
}[number]
3838
>
3939

40-
/*
41-
** Because of pg-meta types generation there is some cases where a same relationship can be duplicated
42-
** if the relation is across schemas and views this ensure that we dedup those relations and treat them
43-
** as postgrest would.
44-
** TODO: This is necessary to allow the check for required desambiguation but shouldn't if the
45-
** generated relationships were correct pointing out that each realation is actually toward a different schema
46-
*/
47-
export type DeduplicateRelationships<T extends readonly unknown[]> = T extends readonly [
48-
infer First,
49-
...infer Rest
50-
]
51-
? First extends Rest[number]
52-
? DeduplicateRelationships<Rest extends readonly unknown[] ? Rest : []>
53-
: [First, ...DeduplicateRelationships<Rest extends readonly unknown[] ? Rest : []>]
54-
: T
55-
5640
export type ResolveRelationships<
5741
Schema extends GenericSchema,
5842
RelationName extends string,
@@ -126,7 +110,7 @@ export type CheckDuplicateEmbededReference<
126110
RelationsNodes
127111
> extends infer ResolvedRels
128112
? ResolvedRels extends unknown[]
129-
? FindDuplicates<DeduplicateRelationships<ResolvedRels>> extends infer Duplicates
113+
? FindDuplicates<ResolvedRels> extends infer Duplicates
130114
? Duplicates extends never
131115
? false
132116
: Duplicates extends { fieldName: infer FieldName extends string }
@@ -143,7 +127,7 @@ export type CheckDuplicateEmbededReference<
143127
* Returns a boolean representing whether there is a foreign key referencing
144128
* a given relation.
145129
*/
146-
export type HasFKeyToFRel<FRelName, Relationships> = Relationships extends [infer R]
130+
type HasFKeyToFRel<FRelName, Relationships> = Relationships extends [infer R]
147131
? R extends { referencedRelation: FRelName }
148132
? true
149133
: false
@@ -155,7 +139,7 @@ export type HasFKeyToFRel<FRelName, Relationships> = Relationships extends [infe
155139
/**
156140
* Checks if there is more than one relation to a given foreign relation name in the Relationships.
157141
*/
158-
export type HasMultipleFKeysToFRel<FRelName, Relationships> = Relationships extends [
142+
type HasMultipleFKeysToFRel<FRelName, Relationships> = Relationships extends [
159143
infer R,
160144
...infer Rest
161145
]
@@ -182,10 +166,7 @@ type CheckRelationshipError<
182166
direction: 'reverse'
183167
}
184168
? // We check if there is possible confusion with other relations with this table
185-
HasMultipleFKeysToFRel<
186-
RelatedRelationName,
187-
DeduplicateRelationships<Relationships>
188-
> extends true
169+
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
189170
? // If there is, postgrest will fail at runtime, and require desambiguation via hinting
190171
RequireHintingSelectQueryError<
191172
RelatedRelationName,
@@ -203,7 +184,7 @@ type CheckRelationshipError<
203184
}
204185
? HasMultipleFKeysToFRel<
205186
RelatedRelationName,
206-
DeduplicateRelationships<TablesAndViews<Schema>[From]['Relationships']>
187+
TablesAndViews<Schema>[From]['Relationships']
207188
> extends true
208189
? RequireHintingSelectQueryError<From extends string ? From : 'unknown', RelatedRelationName>
209190
: FoundRelation
@@ -255,10 +236,7 @@ type ResolveReverseRelationship<
255236
from: CurrentTableOrView
256237
}
257238
: // If the relation was found via implicit relation naming, we must ensure there is no conflicting matches
258-
HasMultipleFKeysToFRel<
259-
RelatedRelationName,
260-
DeduplicateRelationships<Relationships>
261-
> extends true
239+
HasMultipleFKeysToFRel<RelatedRelationName, Relationships> extends true
262240
? RequireHintingSelectQueryError<
263241
RelatedRelationName,
264242
CurrentTableOrView extends string ? CurrentTableOrView : 'unknown'

test/select-query-parser/types.test-d.ts

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import { expectType } from 'tsd'
99
import { TypeEqual } from 'ts-expect'
1010
import {
11-
DeduplicateRelationships,
1211
FindMatchingRelationships,
1312
FindMatchingTableRelationships,
1413
IsRelationNullable,
@@ -179,56 +178,3 @@ import { ParseQuery } from '../../src/select-query-parser/parser/parser'
179178
type r2 = ProcessNodes<Schema, Row, RelationName, Relationships, ParsedQuery>
180179
expectType<r2>(expected!)
181180
}
182-
183-
// Deduplicate exact sames relationships
184-
{
185-
type rels = [
186-
{
187-
foreignKeyName: 'test_fkey'
188-
columns: ['project_id']
189-
referencedRelation: 'project_subscriptions'
190-
referencedColumns: ['project_id']
191-
},
192-
{
193-
foreignKeyName: 'test_fkey'
194-
columns: ['project_id']
195-
referencedRelation: 'projects'
196-
referencedColumns: ['id']
197-
},
198-
{
199-
foreignKeyName: 'test_fkey'
200-
columns: ['project_id']
201-
referencedRelation: 'projects'
202-
referencedColumns: ['id']
203-
},
204-
{
205-
foreignKeyName: 'test_fkey'
206-
columns: ['project_id']
207-
referencedRelation: 'sls_physical_backups_monitoring'
208-
referencedColumns: ['project_id']
209-
}
210-
]
211-
type expected = [
212-
{
213-
foreignKeyName: 'test_fkey'
214-
columns: ['project_id']
215-
referencedRelation: 'project_subscriptions'
216-
referencedColumns: ['project_id']
217-
},
218-
{
219-
foreignKeyName: 'test_fkey'
220-
columns: ['project_id']
221-
referencedRelation: 'projects'
222-
referencedColumns: ['id']
223-
},
224-
{
225-
foreignKeyName: 'test_fkey'
226-
columns: ['project_id']
227-
referencedRelation: 'sls_physical_backups_monitoring'
228-
referencedColumns: ['project_id']
229-
}
230-
]
231-
232-
type result = DeduplicateRelationships<rels>
233-
expectType<TypeEqual<result, expected>>(true)
234-
}

0 commit comments

Comments
 (0)