Skip to content

Commit b74969d

Browse files
authored
fix(db-postgres): querying on hasMany: true select field in a relationship (#12916)
Fixes#11635
1 parent 39e9519 commit b74969d

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎packages/drizzle/src/queries/getTableColumnFromPath.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,10 @@ export const getTableColumnFromPath = ({
799799
`${tableName}_${tableNameSuffix}${toSnakeCase(field.name)}`,
800800
)
801801

802+
constidColumn=(aliasTable??adapter.tables[tableName]).id
802803
if(locale&&isFieldLocalized&&adapter.payload.config.localization){
803804
constconditions=[
804-
eq(adapter.tables[tableName].id,adapter.tables[newTableName].parent),
805+
eq(idColumn,adapter.tables[newTableName].parent),
805806
eq(adapter.tables[newTableName]._locale,locale),
806807
]
807808

@@ -816,7 +817,7 @@ export const getTableColumnFromPath = ({
816817
})
817818
}else{
818819
addJoinTable({
819-
condition: eq(adapter.tables[tableName].id,adapter.tables[newTableName].parent),
820+
condition: eq(idColumn,adapter.tables[newTableName].parent),
820821
joins,
821822
table: adapter.tables[newTableName],
822823
})

‎test/relationships/config.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ export default buildConfigWithDefaults({
229229
name: 'name',
230230
type: 'text',
231231
},
232+
{
233+
name: 'select',
234+
type: 'select',
235+
hasMany: true,
236+
options: ['a','b','c'],
237+
},
232238
{
233239
name: 'director',
234240
type: 'relationship',

‎test/relationships/int.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,21 @@ describe('Relationships', () => {
429429
expect(result.docs[0].id).toBe(id)
430430
})
431431

432+
it('should allow query hasMany select in relationship',async()=>{
433+
constmovie=awaitpayload.create({collection: 'movies',data: {select: ['a','b']}})
434+
constdoc=awaitpayload.create({
435+
collection: 'directors',
436+
data: {name: 'Mega Director', movie },
437+
})
438+
439+
constres=awaitpayload.find({
440+
collection: 'directors',
441+
where: {'movie.select': {equals: 'a'}},
442+
})
443+
expect(res.docs).toHaveLength(1)
444+
expect(res.docs[0].id).toBe(doc.id)
445+
})
446+
432447
it('should allow 4x deep querying',async()=>{
433448
constmovie_1=awaitpayload.create({
434449
collection: 'movies',

‎test/relationships/payload-types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export interface Screening {
266266
exportinterfaceMovie{
267267
id: string;
268268
name?: string|null;
269+
select?: ('a'|'b'|'c')[]|null;
269270
director?: (string|null)|Director;
270271
updatedAt: string;
271272
createdAt: string;
@@ -730,6 +731,7 @@ export interface ScreeningsSelect<T extends boolean = true> {
730731
*/
731732
exportinterfaceMoviesSelect<Textendsboolean=true>{
732733
name?: T;
734+
select?: T;
733735
director?: T;
734736
updatedAt?: T;
735737
createdAt?: T;

0 commit comments

Comments
 (0)